Instant is a Rust standard-library type for recording a monotonic point on the system's elapsed-time clock. Instant::now() captures a snapshot, usually measured from system boot rather than from a calendar date, so its printed value is opaque on its own. section 17.2
Time an operation
Capture an Instant before work, run the operation, and call elapsed() afterward. The result is a Duration. You can also subtract two Instants directly; Instant implements subtraction by producing a Duration. section 17.2
use std::time::Instant;
fn main() {
let start = Instant::now();
println!("Time elapsed before busy operation: {:?}", start.elapsed());
let mut new_string = String::new();
loop {
new_string.push('წ');
if new_string.len() > 100_000 {
break;
}
}
println!("Operation complete. Time elapsed: {:?}", start.elapsed());
}
This runnable pattern measures elapsed time before and after a busy string-building loop, making the timing workflow concrete. section 17.2
Use Instant when you need elapsed timing:
- Measure how long an operation takes or support a benchmark. section 17.2
- Compare repeated implementations, such as sequential and Rayon-based vector processing. section 17.4
- Avoid treating its debug output as a date; use a different time type for calendar-oriented values. section 17.2
