Skip to content
Justice Tech School

Overflow Handling

Use wrapping_, checked_, saturating_ and overflowing_ methods to choose overflow behaviour.

Example
fn main() {
    let a: u8 = 250;
    println!("{:?} {:?}", a.checked_add(10), a.checked_add(5));
    println!("{} {}", a.wrapping_add(10), a.saturating_add(10));
}

Result

None Some(255)
4 255

Exercise

Safe add.

a.add(1)

Quiz Test

What does checked_add return?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.