Back to questions
JuniorTheoryOccasional
What does the program print?
fn main() {
println!("{:?}", 255u8.checked_add(1));
}None
- Expecting a panic, as with overflow in a debug build:
checked_addnotices the overflow itself and returnsNoneinstead of panicking. - Expecting the sum to wrap around to 0: that is
wrapping_add, whilechecked_addreturnsSomeonly when the result fits.
- What does
250u8.checked_add(5)print with{:?}, and why? - Why would a plain
255u8 + 1in the same program fail to compile?