在 CentOS 上 Rust 编译/运行卡住,常见原因和解决办法如下,你可以按顺序排查:
Rust 编译非常吃内存,尤其 cargo build 或 cargo test。
检查:
free -h
top
解决:
export CARGO_BUILD_JOBS=1
或
cargo build -j 1
CentOS 网络慢或被墙,导致 cargo 卡在下载。
检查:
cargo build -v
解决:
export RUSTUP_DIST_SERVER=https://rsproxy.cn
export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup
export CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
或配置 ~/.cargo/config.toml:
[source.crates-io]
replace-with = "rsproxy"
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
CentOS 默认仓库里的 Rust 很老,容易卡死。
解决:
rustup update
编译时大量小文件读写。
解决:
mkdir /tmp/rust-build
export CARGO_TARGET_DIR=/tmp/rust-build
strace -p $(pgrep cargo)
或
ps aux | grep cargo
如果 D 状态(不可中断睡眠),通常是磁盘/网络问题。
CentOS 7 上某些 crate 会直接卡死或崩溃。
建议:
docker run --rm -it rust:latest
# 1. 限制线程
export CARGO_BUILD_JOBS=1
# 2. 使用国内源
export RUSTUP_DIST_SERVER=https://rsproxy.cn
# 3. 重新编译
cargo clean
cargo build
如果你能告诉我:
cargo build 还是 rustc我可以给你更精准的方案。