std-method

Duration::from_secs, from_millis, from_micros and from_nanos

Choose a constructor that matches the source unit rather than multiplying a raw integer manually. Equal intervals constructed with different units compare equal. Expected stdout (not executed): true Common mistake: Multiplying large integer inputs before checking for overflow.

Formal Syntax & Structure

rust syntaxrust
Duration::from_secs(u64); Duration::from_millis(u64); Duration::from_micros(u64); Duration::from_nanos(u64)

Working Example

Example Snippetrust
fn main() {    use std::time::Duration;    let a = Duration::from_secs(1);    let b = Duration::from_millis(1000);    let c = Duration::from_micros(1_000_000);    let d = Duration::from_nanos(1_000_000_000);    println!("{}", a == b && b == c && c == d);}
Rust Reference