Link-Time Optimization (LTO) in Rust optimizes the entire program at the linking stage to improve performance and reduce binary size.
Link-Time Optimization (LTO) in Rust allows the compiler to optimize code across crate boundaries during the final linking stage, often resulting in smaller binaries and faster execution. Enable it in your Cargo.toml under the [profile.release] section:
[profile.release]
lto = true
Alternatively, enable it for a specific build using the RUSTFLAGS environment variable:
RUSTFLAGS="-C lto" cargo build --release
Link-Time Optimization lets the compiler see your entire program as one big piece instead of separate parts. This allows it to remove unused code and speed up functions that call each other across different files. Think of it like an editor reviewing a whole book at once to fix inconsistencies, rather than checking each chapter in isolation.