- checked_add
Handle overflow and underflow - checked
- checked_add
- Returns and Option that, is either the incremented value wrapped in Some or None.
examples/numbers/handle-overflow-checked-add/src/main.rs
fn main() { let mut num: i8 = 126; println!("{num}"); num = num.checked_add(1).unwrap_or(42); println!("{num}"); num = num.checked_add(1).unwrap_or(42); println!("{num}"); }
126 127 42