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 - overflowing

  • overflowing_add

  • overflowing_add

  • Returns a tuple: the value (possible after overflow, and a boolean indicating if overflow happened)

fn main() {
    let mut num: i8 = 126;
    println!("{num}");

    let mut ok;

    (num, ok) = num.overflowing_add(1);
    println!("{num} {ok}");

    (num, ok) = num.overflowing_add(1);
    println!("{num} {ok}");
}
126
127 false
-128 true