Skip to content
Theory · Variables and Types · lesson 8 of 12

Overflow

A box of type u8 holds numbers from 0 to 255. What happens if you put 260 into it?

A result that does not fit into its type is called an overflow. When the overflow is visible right in the code, the compiler catches it: let level: u8 = 255 + 1; does not build and gives the error error: this arithmetic operation will overflow. But a value often changes while the program runs, and then everything is decided at run time:

Rust
fn main() {
    let mut level: u8 = 250;
    println!("{level}");
    level = level + 10; // ✗
    println!("{level}");
}
TEXT
attempt to add with overflow

Let's go line by line:

  1. level is a mutable variable of type u8 with the value 250, and the program prints 250.
  2. level + 10 gives 260, and such a number does not fit into a u8. Here the compiler does not notice the overflow, and the program builds.
  3. At run time the program stops with the panic attempt to add with overflow, and the last line does not run.

Debug and release builds

The panic happens in the debug build that cargo run makes: it checks every addition, subtraction and multiplication. The release build, cargo run --release, has none of these checks for the sake of speed, and instead of a panic the same program quietly prints 4.

Here the box is a poor picture: an overfull real box spills what does not fit. But in a release build the number goes around in a circle, like a car's odometer: after 255 comes 0 again, and 260 becomes 4.

Interviewers often ask this: what happens when an integer overflows in Rust? The answer:

  • if the overflow is visible in the code, the program does not build;
  • at run time, a debug build stops with a panic;
  • in a release build, the number quietly goes around the circle.

The danger is that a program is usually tested in a debug build, while in a release build the same error goes unnoticed.

Overflow without a panic

If a number might not fit, you choose the behavior yourself. For this, every integer type has methods: actions you call with a dot after the value, like len() on text:

Rust
let level: u8 = 250;
println!("{:?}", level.checked_add(5)); // Some(255)
println!("{:?}", level.checked_add(10)); // None
println!("{}", level.wrapping_add(10)); // 4
println!("{}", level.saturating_add(10)); // 255

checked_add: check

checked_add returns not a number but an answer in one of two forms. Some(255) means "there is a result, here it is", and None means "the result did not fit". Such an answer is printed with {:?}. The method fits when an overflow is an error you need to notice.

wrapping_add: around the circle

wrapping_add always goes around the circle, like a release build: 250 + 10 gives 4. It is used where passing through zero is intended in advance.

saturating_add: up to the limit

saturating_add stops at the largest value of the type: for u8 that is 255. A volume knob works this way: the sound does not go above the maximum.

Subtraction and multiplication have the same methods: checked_sub, wrapping_mul and others.

Key points

  • An overflow visible in the code is caught by the compiler; at run time a debug build panics, and in a release build the number goes around the circle.
  • checked_add returns Some(sum) or None, wrapping_add goes around the circle, saturating_add stops at the limit.
  • A program is usually tested in a debug build, and in a release build the same overflow error passes silently.