在 Debian 上“开启 Rust 优化”通常有几层含义,下面按常见场景给你一套实用做法。
默认 cargo build 是 debug 模式,几乎无优化。
cargo build --release
优化级别由 Cargo.toml 控制:
[profile.release]
opt-level = 3
lto = true
codegen-units = 1
panic = "abort"
strip = true
说明:
opt-level = 3:最高优化lto = true:链接期优化codegen-units = 1:更好的优化(稍慢编译)strip = true:去掉调试符号,减小体积在 ~/.bashrc 或 ~/.profile 中:
export RUSTFLAGS="-C target-cpu=native -C opt-level=3"
target-cpu=native:针对当前 CPU 优化cargo build / install 都生效⚠️ 注意:
这样编译的二进制不能跨机器移植
不要用 apt 的旧版本 Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
然后:
source "$HOME/.cargo/env"
如果你在打 .deb 包:
cargo build --release
并在 debian/rules 或构建脚本中确保:
debugRUSTFLAGSsudo apt install mold
export RUSTFLAGS="-C link-arg=-fuse-ld=mold"
仅适合 debug:
rustup component add rustc-codegen-cranelift-preview
CARGO_PROFILE_DEV_CODEGEN_BACKEND="cranelift" cargo build
cargo build --release --verbose
或查看二进制:
file target/release/your_bin
如果你能说明:
我可以给你更精确的配置。