liveBook · Manning

Integer wraparound

As explained in Code Like a Pro in Rust

Integer wraparound is arithmetic behavior in which an out-of-range fixed-width integer result cycles or is reduced modulo the type’s representable range.

Integer wraparound reduces an out-of-range result modulo the fixed-width type’s range. In Rust, use explicit wrapping operations when this behavior is intended rather than relying on the arithmetic behavior of a particular build profile. section 4.5.1 section 6.3

assert_eq!(0xffu8.wrapping_add(1), 0);
assert_eq!(0xffffffffu32.wrapping_add(1), 0);
assert_eq!(0u32.wrapping_sub(1), 0xffffffff);
assert_eq!(0x80000000u32.wrapping_mul(2), 0);

These unsigned examples wrap addition, subtraction, and multiplication at the type’s boundary. section 4.5.1

Under the default settings described here, debug builds check arithmetic overflow, while release builds use unchecked arithmetic. A test can therefore panic on overflow even when the same operation produces no error in production. Explicit wrapping makes the intended behavior clear across those contexts. section 6.3

Rust provides wrapping methods and the `Wrapping` struct. For generic wrapping addition, `num_traits::WrappingAdd` supplies a trait bound; a property test can compare the generic function against the primitive’s `wrapping_add` result. section 6.3

extern crate num_traits;                                    #1
use num_traits::ops::wrapping::WrappingAdd;
 
pub fn add<T: WrappingAdd<Output = T>>(a: T, b: T) -> T {   #2
    a.wrapping_add(&b)
}
 
#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;
    proptest! {
        #[test]
        fn test_add(a: i64, b: i64) {
            assert_eq!(add(a, b), a.wrapping_add(b));       #3
        }
    }
}

The generic adder uses `WrappingAdd`, and its property test uses explicit wrapping arithmetic for the expected result. section 6.3

Choose wrapping only when modular results are wanted. Checked operations offer a different contract: for example, `checked_div` returns `None` for division by zero. When matching C behavior, remember that signed integer overflow in C is undefined. section 4.5.1

How Code Like a Pro in Rust covers it

How deep it goes
Demonstrates primitive and generic wrapping operations, contrasting them with checked arithmetic and build-profile overflow behavior.
Examples
Shows unsigned boundary operations and an `i64` property test that first fails on overflow, then passes with explicit wrapping.
What you can do after
Apply wrapping arithmetic using Rust primitive methods, `Wrapping`, or `num_traits::WrappingAdd` for generic addition.
How it connects
Connects wraparound to modular arithmetic, overflow checking, generic traits, and property-based testing.

Also explained in

Compare how 3 books explain integer wraparound →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.