Sub topic 10
Debug builds versus release builds
This chapter assumes you have Rust installed with rustup, can run cargo new and cargo run, and know what Cargo.toml is. Here you will see how Cargo's two default build modes differ, where each puts its output, and when to use each one.
Two profiles, two goals
Cargo builds a project with a profile, which is a set of compiler settings. Two profiles cover most daily work.
- The dev profile is the default. It compiles quickly and keeps debugging information, so you can step through code in a debugger. It does little optimization, so the program runs slower.
- The release profile spends more compile time on optimization. The program runs faster, but the build takes longer and is harder to debug.
You choose a profile with a flag:
cargo build
cargo build --releaseThe first command makes a debug build. The second makes a release build.
Where the output goes
Each profile writes to its own directory under target, so the two builds never overwrite each other.
| Command | Output directory | Typical use |
|---|---|---|
| `cargo build` | `target/debug` | Everyday development |
| `cargo build --release` | `target/release` | Benchmarks and shipping |
For a project named hello_cargo, the executables are target/debug/hello_cargo and target/release/hello_cargo. On Windows, both files end in .exe.
cargo run also accepts the flag. cargo run builds and runs the debug binary, and cargo run --release does the same with the release binary.
cargo run
cargo run --releaseA program that shows the difference
Integer overflow is a good example of a difference you can observe. In a debug build, Rust adds checks that stop the program with a panic if an integer overflows. In a release build, those checks are off by default, and the value wraps around.
Create a project and replace src/main.rs:
cargo new profile_demo
cd profile_demofn main() {
let mut counter: u8 = 250;
for _ in 0..10 {
counter += 1;
}
println!("counter = {counter}");
}A u8 holds values from 0 to 255, so adding 10 to 250 overflows it. Run it both ways:
cargo run
cargo run --releaseThe debug run panics with the message attempt to add with overflow. The release run prints counter = 4, because the value wrapped past 255 back to 0.
Adjusting a profile
You can change a profile's settings in Cargo.toml. The opt-level setting controls optimization from 0 (none) to 3 (most). The defaults are 0 for dev and 3 for release.
[profile.dev]
opt-level = 1
[profile.release]
overflow-checks = trueThis example makes debug builds slightly faster to run and turns the overflow checks back on in release builds. Change these only when you have a measured reason. The defaults suit most projects.
Which one to use
- Use
cargo checkwhile editing. It type-checks without producing a binary, so it is faster than either build. - Use
cargo runandcargo buildduring development, where fast compiles matter most. - Use
--releasewhen you measure speed, run heavy workloads, or hand a binary to someone else. Timing a debug build tells you very little about real performance.
Exercise
You run cargo build --release in a project named notes. Where is the executable, and why might the same code behave differently from cargo run?
Show answer
The executable is target/release/notes. The release profile optimizes the code and disables integer overflow checks by default, while cargo run uses the dev profile, which has no optimization and keeps overflow checks. Code that overflows can panic in the debug build and wrap silently in the release build.
Exercise
Which setting in Cargo.toml would you change to turn integer overflow checks on for release builds, and under which table does it go?
Show answer
Set overflow-checks = true under the [profile.release] table.