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


