Minimize Rust binary size for embedded systems by enabling LTO, setting opt-level to z, and stripping symbols in the release profile.
Minimize binary size by compiling with the release profile and enabling Link-Time Optimization (LTO) in your Cargo.toml.
- Add the LTO configuration to your
[profile.release] section in Cargo.toml.
[profile.release]
lto = true
opt-level = "z"
strip = true
- Build your project using the release profile to apply these optimizations.
cargo build --release
- Verify the reduced binary size by checking the file in the
target/release directory.
ls -lh target/release/your_binary_name
Minimizing binary size for embedded Rust shrinks your program's file size by telling the compiler to aggressively remove unused code and optimize for space rather than speed. Think of it like packing a suitcase for a trip: you remove everything you don't need and fold the remaining clothes tightly to fit more in less space. You use this when deploying to devices with limited storage, like microcontrollers or embedded systems.