温馨提示×

centos rust卡住怎么办

小樊
69
2026-10-17 11:13:30
栏目: 编程语言

在 CentOS 上 Rust 编译/运行卡住,常见原因和解决办法如下,你可以按顺序排查:


一、最常见原因

1️⃣ 内存不足(OOM / 卡死)

Rust 编译非常吃内存,尤其 cargo build 或 cargo test。

检查:

free -h
top

解决:

  • 加内存 / 加 swap
  • 限制并行编译:
export CARGO_BUILD_JOBS=1

或

cargo build -j 1

2️⃣ 网络卡住(下载依赖)

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"

3️⃣ rustup / cargo 版本太旧

CentOS 默认仓库里的 Rust 很老,容易卡死。

解决:

rustup update

4️⃣ 磁盘 IO 慢(HDD / 云盘)

编译时大量小文件读写。

解决:

  • 换 SSD
  • 或在内存盘编译:
mkdir /tmp/rust-build
export CARGO_TARGET_DIR=/tmp/rust-build

二、调试是否真“卡住”

strace -p $(pgrep cargo)

或

ps aux | grep cargo

如果 D 状态(不可中断睡眠),通常是磁盘/网络问题。


三、CentOS 特有坑

5️⃣ glibc / 系统太老

CentOS 7 上某些 crate 会直接卡死或崩溃。

建议:

  • CentOS 7 → 升级到 CentOS Stream / Rocky / Alma
  • 或用容器:
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

如果你能告诉我:

  • CentOS 版本(7 / 8 / Stream)
  • 卡在 cargo build 还是 rustc
  • CPU / 内存大小

我可以给你更精准的方案。

0 踩