Release Mode Versus Debug Mode in Rust and the Speed Gap
cargo build is slow to run and fast to compile, cargo build --release is the reverse. See what each profile changes, how to measure the gap and how to tune the dev profile.

A test that took two seconds in debug takes forty milliseconds in release, and the first thing to learn about Rust performance is to not judge either by the other. The two builds are the same compiler with different profile settings, and the settings matter more than they do in most languages because Rust leans on the optimizer to remove abstraction. This post lists what differs, how to measure the gap yourself and how to make debug usable. It extends Rust performance basics.
Rust 1.90.
What the profiles set
| Setting | dev | release |
|---|---|---|
opt-level | 0 | 3 |
debug-assertions | on | off |
overflow-checks | on | off |
debug | full | none |
incremental | on | off |
codegen-units | 256 | 16 |
lto | off | off |
With opt-level = 0 the compiler does almost no inlining, so an iterator chain becomes a series of real function calls. At opt-level = 3 the same chain often compiles to a tight loop.
Measure the gap
fn main() {
let start = std::time::Instant::now();
let sum: u64 = (0..200_000_000u64).filter(|n| n % 3 == 0).map(|n| n * 2).sum();
println!("{sum} in {:?}", start.elapsed());
}cargo run
cargo run --releaseThe output has the same sum in both, and a much smaller time in release. We do not give numbers because the ratio depends on the CPU; run it and compare, then try the same with a loop that does file I/O to see a smaller gap. The release run may constant-fold or vectorize the loop, which is why a toy example overstates the difference for real programs.
Behavior differences
fn main() {
let x: u8 = 250;
let y = x + std::hint::black_box(10);
println!("{y}");
}In debug this panics with attempt to add with overflow. In release it prints 4, because overflow wraps. If your code depends on overflow behavior, use checked_add, wrapping_add or saturating_add explicitly. More on that in integer overflow bugs in release builds. debug_assert! also disappears in release.
Make debug workable
[profile.dev]
opt-level = 1
[profile.dev.package."*"]
opt-level = 3This optimizes all dependencies at the highest level (they rarely change, so it is a one-time cost) and your own code lightly. Many projects find parsing, hashing and serialization crates unusable at opt-level = 0. You keep debug info and fast rebuilds for your own crate.
Release options that matter
lto = "thin", codegen-units = 1 and panic = "abort" trade compile time for speed and size; see release profile settings. Add debug = "line-tables-only" to keep readable profiles (profiling guide).
Limits
Do not deploy a debug build. Do not benchmark one. Do not conclude that Rust is slow from one. For the profile reference, see the Cargo book's chapter on profiles.


