std-method

Duration::checked_mul and checked_div

Scale a duration with integer arithmetic. Multiplication detects an unrepresentable result; division rejects a zero divisor and truncates sub-nanosecond fractions. This suits retry budgets and batch timing. Expected stdout (not executed): 500 true 2 Common mistake: Assuming integer division preserves fractional nanoseconds.

Formal Syntax & Structure

rust syntaxrust
d.checked_mul(rhs: u32) -> Option<Duration>; d.checked_div(rhs: u32) -> Option<Duration>

Working Example

Example Snippetrust
fn main() {    use std::time::Duration;    let d = Duration::from_millis(125);    println!("{}", d.checked_mul(4).unwrap().as_millis());    println!("{}", d.checked_div(0).is_none());    println!("{}", Duration::from_nanos(5).checked_div(2).unwrap().as_nanos());}
Rust Reference