Why are Rust executables so huge?

后端 未结 6 1361
醉酒成梦
醉酒成梦 2020-11-30 17:34

Just having found Rust and having read the first two chapters of the documentation, I find the approach and the way they defined the language particularly interesting. So I

6条回答
  •  隐瞒了意图╮
    2020-11-30 18:21

    For an overview of all of the ways to reduce the size of a Rust binary, see the min-sized-rust repository.

    The current high level steps to reduce binary size are:

    1. Use Rust 1.32.0 or newer (which doesn't include jemalloc by default)
    2. Add the following to Cargo.toml
    [profile.release]
    opt-level = 'z'     # Optimize for size.
    lto = true          # Enable Link Time Optimization
    codegen-units = 1   # Reduce number of codegen units to increase optimizations.
    panic = 'abort'     # Abort on panic
    
    1. Build in release mode using cargo build --release
    2. Run strip on the resulting binary.

    There is more that can be done using nightly Rust, but I'll leave that information in min-sized-rust as it changes over time due to the use of unstable features.

    You can also use #![no_std] to remove Rust's libstd. See min-sized-rust for details.

提交回复
热议问题