How to Add and Subtract Time in Rust

Add and subtract time in Rust by using standard arithmetic operators on numeric duration values.

Use the + and - operators on numeric types like i32 or f64 to add and subtract values directly. Rust does not have a built-in Time type for arithmetic in the standard library, so you typically perform math on durations (like milliseconds) or use a crate like chrono for calendar dates.

fn main() {
    let start = 10.5; // e.g., seconds or milliseconds
    let duration = 2.3;
    let end = start + duration;
    let previous = start - duration;
    println!("End: {end}, Previous: {previous}");
}