Build & Inspect Tooling
Beyond cargo build, Linux tooling inspects Rust binaries: size breakdown, symbols, linking mode, and strip policies for deployment.
Recipe
cargo build --release --locked
file target/release/my-service
ldd target/release/my-service 2>/dev/null || true
cargo bloat --release -n 20
strip target/release/my-service
ls -lh target/release/my-serviceWhen to reach for this:
- Image size regression investigation
- Verifying static musl binary
- Debugging missing symbols in prod stack traces
Working Example
cargo install cargo-bloat
cargo bloat --release --crates -n 15
# Inspect symbols
nm -C target/release/my-service | rg "my_service::" | head
# Disassemble hot function (advanced)
objdump -d --demangle target/release/my-service | less# Cargo.toml - release profile
[profile.release]
strip = "symbols"
lto = "thin"
codegen-units = 1What this demonstrates:
cargo bloatattributes size to cratesnmlists mangled/demangled symbols- Profile
stripautomates post-link shrink
Deep Dive
cargo subcommands
| Command | Purpose |
|---|---|
cargo tree -e features | Why dependency exists |
cargo llvm-lines | LLVM IR size proxy |
cargo audit | Security advisories |
cargo deny check | License and ban policy |
Gotchas
- Stripping debug symbols - harder prod backtraces. Fix: split debug info in CI artifact store.
- bloat on dev build - misleading sizes. Fix: always
--release. - ldd on static binary - prints "not a dynamic executable" - that is expected.
- objdump on PIE - addresses differ per run - use offsets for docs only.
- Ignoring
build.rs- native link flags hidden. Fix: readbuild.rswhen inspecting link.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
samply / perf | Runtime profiling | Static size only |
twiggy | WASM size | Native only |
dwarfdump | Debug info analysis | Quick check |
FAQs
How small should CLI be?
Stripped release often 2-8 MB depending on deps; investigate if sudden doubling.
cargo bloat vs tree?
bloat measures linked size contribution; tree shows dependency graph only.
Related
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+.