Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Handle overflow and underflow - checked

  • checked_add

  • checked_add

  • Returns and Option that, is either the incremented value wrapped in Some or None.

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