std-method
Duration::as_secs, as_millis, as_micros and as_nanos
These integer accessors report the whole number of units across the entire duration. Fractions smaller than the selected unit are discarded. The wide return type avoids overflowing the valid Duration range. Expected stdout (not executed): 2 2345 2345678 2345678901 Common mistake: Confusing whole milliseconds with the fractional millisecond component.
Formal Syntax & Structure
rust syntaxrust
d.as_secs() -> u64; d.as_millis() -> u128; d.as_micros() -> u128; d.as_nanos() -> u128Working Example
Example Snippetrust
fn main() { use std::time::Duration; let d = Duration::new(2, 345_678_901); println!("{} {} {} {}", d.as_secs(), d.as_millis(), d.as_micros(), d.as_nanos());}