expression

Integer overflow policies

Choose how a fixed-width counter handles a result beyond its range. For a byte, 250 + 10 is mathematically 260, which cannot fit in u8. The four methods express different intentions, independent of ordinary addition overflow settings. Expected stdout (not executed): None 255 4 (4, true) Common mistake: Assuming ordinary + always panics or always wraps; overflow-check settings matter.

Formal Syntax & Structure

rust syntaxrust
x.checked_add(y); x.saturating_add(y); x.wrapping_add(y); x.overflowing_add(y)

Working Example

Example Snippetrust
fn main() {    let n = 250u8;    println!("{:?}", n.checked_add(10));    println!("{}", n.saturating_add(10));    println!("{}", n.wrapping_add(10));    println!("{:?}", n.overflowing_add(10));}
Rust Reference