Answer#
A Rust executable is statically linked: the parts of the standard library it uses, the code of every dependency, and generic functions compiled separately for each type they are used with are all inside the file. A debug build is much larger still, because it has no optimisation and (on Linux, for example) carries debug symbols in the file.
The release profile in Cargo.toml controls how much of that is removed. These settings go under [profile.release]:
| Setting | Effect | Trade-off |
|---|---|---|
lto = true | Optimises across crate boundaries and removes unused code | Slower release builds |
codegen-units = 1 | Lets the optimiser see the whole crate at once | Slower release builds, no parallel code generation |
panic = "abort" | A panic ends the process; the unwinding tables are not included | catch_unwind no longer works; destructors do not run on a panic |
opt-level = "z" | Optimise for size instead of speed | The program may be slower |
strip = true | Removes symbol and debug information from the file | No effect when the symbols are already in a separate .pdb file (see the result below) |
How to do it#
- Measure first. This is the test program: it counts words from its command line arguments (it uses
HashMap, sorting and formatting, so it is not an empty program).
use std::collections::HashMap;
fn main() {
let text = std::env::args().skip(1).collect::<Vec<_>>().join(" ");
let mut counts: HashMap<String, usize> = HashMap::new();
for w in text.split_whitespace() {
*counts.entry(w.to_lowercase()).or_default() += 1;
}
let mut v: Vec<_> = counts.into_iter().collect();
v.sort();
for (w, n) in v {
println!("{w}: {n}");
}
}- Add the profile, one setting at a time, and run
cargo build --releaseafter each:
[profile.release]
strip = true
lto = true
codegen-units = 1
panic = "abort"
opt-level = "z"Measured result#
Machine and versions: Windows 11 (10.0.26300), rustc 1.93.0, the MSVC toolchain, one run per row, size of target\release\size.exe.
Settings in [profile.release] | Size in bytes |
|---|---|
| none (the default release profile) | 175,104 |
strip = true | 175,104 |
plus lto = true, codegen-units = 1 | 163,328 |
plus panic = "abort" | 154,112 |
plus opt-level = "z" | 144,896 |
That is about 17 percent smaller for a program this small. Two honest observations: strip = true changed nothing on this Windows build, because the MSVC toolchain keeps debug information in a separate .pdb file rather than in the executable (on Linux it can matter much more); and for a tiny program the standard library dominates the size, so the savings are largest on bigger programs with many dependencies. Measure your own program rather than expecting these numbers.
Common mistakes#
- Using
opt-level = "z"on code where speed matters, and then not measuring the speed. - Using
panic = "abort"in a library or a server that relies on catching panics. - Compressing the file with a packer to hide the size. It can trigger antivirus warnings and does not reduce what is loaded at run time.
Related questions#
Why is the debug build so much bigger than the release build?#
The debug profile does no optimisation and keeps checks such as integer overflow detection, so the compiled code is larger and slower. Use cargo build --release for anything you measure or ship.
Does cargo build --release produce the same file everywhere?#
No. The result depends on the target (operating system and processor), the Rust version and the profile. Build on or for each target you ship.
Sources#
- The Cargo Book, "Profiles": https://doc.rust-lang.org/cargo/reference/profiles.html