“Debian 上的 Rust 很慢”通常不是 Rust 语言本身慢,而是在 Debian 环境里编译/运行 Rust 程序时感觉慢,常见原因有下面几类:
Debian 稳定版(stable)为了稳定性,Rust 版本往往落后很多:
后果
rustc 性能改进✅ 解决:用 rustup 安装官方 Rust,而不是 apt install rustc
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Rust 是 AOT + 强类型 + 零成本抽象,编译本身就很重。
Debian 上更慢的原因:
cargo build
默认会用多核,但如果:
CARGO_BUILD_JOBS=1就会很慢。
✅ 检查:
nproc
cargo run
是 debug 模式:
✅ 发布模式:
cargo build --release
cargo run --release
Debian 默认用 ld(GNU ld),Rust 链接阶段慢。
✅ 改用 mold 或 lld:
apt install mold
RUSTFLAGS="-C link-arg=-fuse-ld=mold" cargo build --release
或永久配置:
# .cargo/config.toml
[build]
rustflags = ["-C", "link-arg=-fuse-ld=mold"]
Debian 的 Rust 包:
rust-stdcargo 或版本不匹配✅ 推荐:永远用 rustup,不用 apt
如果你在:
Rust 的 target/ 目录有上万小文件,会非常慢。
✅ 解决:
sccachecargo install sccache
export RUSTC_WRAPPER=sccache
Rust 程序运行本身极快,但:
这通常不是 Rust 的问题。
Debian 上 Rust 慢,90% 是因为:旧版本 + debug 模式 + 系统 linker + apt 安装方式
如果你愿意,可以告诉我:
apt 还是 rustup我可以给你一套最优配置。