Reducing Binary Size
Rust binaries can grow large from monomorphization and debug symbols. Strip symbols, tune opt-level, disable unused features, and use panic = abort for smaller deploy artifacts.
Search across all documentation pages
Rust binaries can grow large from monomorphization and debug symbols. Strip symbols, tune opt-level, disable unused features, and use panic = abort for smaller deploy artifacts.
[profile.release]
opt-level = "z"
lto = true
panic = "abort"
strip = "symbols"cargo bloat --release --cratesWhen to reach for this: CLI distribution, embedded targets, WASM bundles, and container image slimming.
[profile.release]
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"
strip = "symbols"
[features]
default = []cargo build --release
ls -lh target/release/myapp
cargo bloat --release -n 30What this demonstrates:
opt-level = "z" optimizes for size over speedcargo bloat shows largest functions/cratesstrip removes symbol tables from shipped binary| Lever | Effect |
|---|---|
| Fewer features on deps | Less code linked |
panic = abort | No unwinding tables |
strip | Smaller on disk |
| Dynamic linking (platform) | Rare in Rust servers |
upx compression | Last resort, AV issues |
[profile.release]
opt-level = "s"Run wasm-opt -Oz after build for WASM targets.
rt needed. Fix: default-features = false.panic = abort; libs usually unwind.| Alternative | Use When | Don't Use When |
|---|---|---|
opt-level = "s" | Balance size/speed | Extreme size need |
split-debuginfo | Keep symbols off binary | Need single-file debug |
| Static musl binary | Portable CLI | glibc DNS nuances |
Context-dependent: CLI < few MB ideal; server less critical than container count of deps.
Yes, merges and dead-strips unused functions across crates.
Enable only needed features: rt-multi-thread, macros, not full unless required.
Use clap 4 with derive and disable unused string features; consider argh for tiny CLIs.
Necessary for JSON APIs; avoid deriving on every internal type if not serialized.
strip for minimal artifact; split for crash reporting with external symbols.
wasm-pack build --release + wasm-opt; avoid pulling full std when possible.
Larger than dynamic glibc sometimes; trade portability vs MB.
cargo bloat shows instantiations; reduce generic types in hot crate surface.
Small runtime image complements stripped Rust binary; still minimize binary itself.
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Reviewed by Chris St. John·Last updated Jul 16, 2026