Handle overflow and underflow - overflowing
-
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