默認(rèn)情況下,Rust編譯器針對(duì)執(zhí)行速度、編譯速度和調(diào)試的簡(jiǎn)易性(例如,通過包括符號(hào))進(jìn)行優(yōu)化,而不是針對(duì)最小二進(jìn)制大小進(jìn)行優(yōu)化。
當(dāng)前減少二進(jìn)制大小的高級(jí)步驟包括:
1、使用Rust 1.32.0或更高版本(默認(rèn)情況下不包括jemalloc
)
2、將以下內(nèi)容添加到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 strip = true # Strip symbols from binary
3、配置后再進(jìn)行二進(jìn)制生成操作:
cargo build --release
要了解更多優(yōu)化rust二進(jìn)制文件的方法,請(qǐng)參閱:How to minimize Rust binary size