std-method
Integer overflowing_add and overflowing_sub
Keep the wrapped result and separately inspect whether the mathematical result exceeded the type. Expected stdout (not executed): 6 true (254, true) Common mistake: Ignoring the flag when overflow is an error.
Formal Syntax & Structure
rust syntaxrust
x.overflowing_add(y) -> (Self, bool); x.overflowing_sub(y)Working Example
Example Snippetrust
fn main() { let (next, wrapped) = 250u8.overflowing_add(12); println!("{next} {wrapped}"); println!("{:?}", 3u8.overflowing_sub(5));}