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:
fn main() {
let mut level: u8 = 250;
println!("{level}");
level = level + 10; // ✗
println!("{level}");
}attempt to add with overflowLet's go line by line:
levelis a mutable variable of typeu8with the value250, and the program prints250.level + 10gives260, and such a number does not fit into au8. Here the compiler does not notice the overflow, and the program builds.- 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:
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)); // 255checked_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_addreturnsSome(sum)orNone,wrapping_addgoes around the circle,saturating_addstops at the limit.- A program is usually tested in a debug build, and in a release build the same overflow error passes silently.