std-method

Instant::duration_since and checked_duration_since

Find the elapsed interval between readings. The ordinary method currently clamps reversed readings to zero, while checked_duration_since reports None. Choose the checked form when incorrect ordering must be detected; platform monotonicity failures can also produce None. Expected stdout (not executed): 80 true Common mistake: Silently accepting reversed readings when an ordering mistake should fail.

Formal Syntax & Structure

rust syntaxrust
later.duration_since(earlier: Instant) -> Duration; later.checked_duration_since(earlier) -> Option<Duration>

Working Example

Example Snippetrust
fn main() {    use std::time::{Duration, Instant};    let a = Instant::now();    let b = a.checked_add(Duration::from_millis(80)).unwrap();    println!("{}", b.duration_since(a).as_millis());    println!("{}", a.checked_duration_since(b).is_none());}
Rust Reference