I'm writing a program that's quite compute heavy, and it's annoyingly slow to run in debug mode.
My program is also plagued by integer overflows, because I'm reading data from u8
arrays and u8
type spreads to unexpected places via type inference, and Rust prefers to overflow rather than to promote integers to larger types.
Building in release mode disables overflow checks:
cargo run --release
How can I build Rust executable with optimizations and runtime overflow checks enabled as well?
If your code relies on overflow occurring, use wrapping_add , or the Wrapping type.
You'll only compile in release mode once, but you'll run the compiled program many times, so release mode trades longer compile time for code that runs faster. That is why the default opt-level for the release profile is 3 .
You can compile in release mode with overflow checks enabled:
[profile.release]
overflow-checks = true
This passes -C overflow-checks=true
to the compiler. In earlier versions of Rust, overflow-checks
was part of the debug-assertions
switch, so you may need to use that in certain cases.
Other times, the easiest thing might be to build in test or dev mode with optimizations:
[profile.dev]
opt-level = 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With