expression
Make a unit conversion explicit at the API boundary
Newtypes can make units explicit. Implement From only when every possible source value can be converted without failure or loss relevant to the contract. Here u32 seconds always fit into u64 milliseconds. A fallible or rounding conversion should instead expose that behavior deliberately. Expected result (NOT RUN): 3000 ms Common mistake: Using implicit numeric conventions for units, or implementing From for conversions that can fail. Static Rust example; not a verified ByteMind executable assessment.
Formal Syntax & Structure
rust syntaxrust
impl From<Seconds> for MillisecondsWorking Example
Example Snippetrust
struct Seconds(u32);struct Milliseconds(u64);impl From<Seconds> for Milliseconds { fn from(value: Seconds) -> Self { Self(u64::from(value.0) * 1000) }}fn duration(value: Milliseconds) -> u64 { value.0 }fn main() { println!("{} ms", duration(Seconds(3).into())); }