Search code examples
rustinteger-overflow

How can integer overflow protection be turned off?


My default Rust has integer overflow protect enabled, and will halt a program in execution on overflow. A large number of algorithms require overflow to function correctly (SHA1, SHA2, etc.)


Solution

  • Use the Wrapping type, or use the wrapping functions directly. These disable the overflow checks. The Wrapping type allows you to use the normal operators as usual.

    Also, when you compile your code in "release" mode (such as with cargo build --release), the overflow checks are omitted to improve performance. Do not rely on this though, use the above type or functions so that the code works even in debug builds.