How create smaller Rust binaries

This is a small tutorial on how to lower your Rust binary size

Edit your Cargo.toml to contain:

[profile.release]
lto = true
incremental = false # Incremental compilation doesnt work with LTO
panic = 'abort' # Disable unwind
opt-level = "z" # Optimize for size, you might want to remove this if performance is key

Build with RUSTFLAGS="-C link-arg=-s" cargo build --release to strip the end binary from debug symbols

Impact:

On a single source file binary with about 4 direct libraries (clap, dirs, reqwest, lazy_static)

50M     target/debug/ufoxrs
5.6M    target/release/ufoxrs (no optimizations)
3.4M    target/release/ufoxrs (strip)
2.7M    target/release/ufoxrs (+ panic=abort)
2.5M    target/release/ufoxrs (+ LTO)
1.7M    target/release/ufoxrs (+ opt-level=z)