Instant vs SystemTime
`Instant` is a monotonic clock — it never goes backwards even if the system clock changes. Use it for measuring elapsed time, benchmarks, and timeouts. `SystemTime` is the wall clock — adjustable by NTP, daylight savings, manual changes — use it only when you actually need a date / time, not just an elapsed interval.
Duration arithmetic
`Duration` is the difference between two Instants or between two SystemTimes. It supports `+`, `-`, `*` by integer, and conversion via `as_secs`, `as_millis`, `as_micros`, `as_nanos`. Use `Duration::from_secs`, `from_millis`, etc. for construction. Operations saturate at the max representable value rather than overflowing.
For deeper background, see a complete cheat-sheet of std collection complexities for the broader context behind this section.
Timing a block of code
The standard recipe: `let start = Instant::now(); /* work */; let elapsed = start.elapsed();`. For statistically meaningful microbenchmarks, prefer the `criterion` crate, which handles warm-up, repetition, and statistical analysis.
Sleeping and parking
`std::thread::sleep(duration)` blocks the current thread for at least that long. The kernel may park you for slightly longer; the call never wakes early. For waking another thread on a deadline use `Condvar::wait_timeout` or, in async code, `tokio::time::sleep`.
For deeper background, see the official Rust API guidelines for module-level design for the broader context behind this section.
Common pitfalls
Subtracting `SystemTime` values can fail if the clock jumped backwards — always handle the `Result` returned by `duration_since`. `Instant` is opaque — don't try to compare across processes, save it to disk, or use it for absolute timing.