Reduce Rust binary size by building in release mode with link-time optimization and stripping debug symbols.
Compile your Rust project in release mode with optimizations enabled to significantly reduce binary size. Run the following command to build a stripped, optimized binary:
RUSTFLAGS="-C link-arg=-s" cargo build --release
This command enables the lto (Link Time Optimization) and strips debug symbols, resulting in a much smaller executable.
Think of this like packing a suitcase for a trip. When you build for development, you pack extra tools and maps (debug info) that help you fix problems but take up space. When you build for release with these flags, you only pack the essentials needed to run, removing the extra weight to make the final package smaller and faster to ship.