以下是在Ubuntu上优化Rust编译速度的方法:
rustup update
export RUSTC_THREADS=8 # 或在~/.cargo/config.toml中添加
cargo install mold
export RUSTFLAGS="-C linker=mold"
opt-level=0
或1
,发布阶段用3
并启用LTO(链接时优化)。# Cargo.toml
[profile.release]
opt-level = 3
lto = true
cargo-udeps
清理未使用依赖,搭配sccache
缓存编译结果。cargo install cargo-udeps sccache
export RUSTC_WRAPPER=sccache
Rayon
并行化可并行代码段。参考资料: