std-method
Duration::checked_add: prevent interval overflow
Return the sum if representable, otherwise None. This is useful for combining budgets whose magnitude comes from configuration. Unlike ordinary addition, overflow is reported as an option. Expected stdout (not executed): 1000 true Common mistake: Using + for untrusted durations without a range check.
Formal Syntax & Structure
rust syntaxrust
d.checked_add(rhs: Duration) -> Option<Duration>Working Example
Example Snippetrust
fn main() { use std::time::Duration; let a = Duration::from_millis(250); let b = Duration::from_millis(750); println!("{}", a.checked_add(b).unwrap().as_millis()); println!("{}", Duration::MAX.checked_add(a).is_none());}