How to Minimize Binary Size for Embedded Rust

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.

  1. Add the LTO configuration to your [profile.release] section in Cargo.toml.
[profile.release]
lto = true
opt-level = "z"
strip = true
  1. Build your project using the release profile to apply these optimizations.
cargo build --release
  1. Verify the reduced binary size by checking the file in the target/release directory.
ls -lh target/release/your_binary_name