温馨提示×

Debian跑Rust如何配置

小樊
49
2026-09-29 07:50:33
栏目: 编程语言

在 Debian 上运行 / 开发 Rust,一般分三步:安装工具链 → 配置环境 → 跑通示例。下面按「推荐做法 + 可选方案」给你一份清晰配置指南。


一、推荐方式:使用 rustup(官方推荐)

1. 安装依赖

sudo apt update
sudo apt install -y curl build-essential gcc

2. 安装 Rust(rustup)

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

安装过程中选择:

  • 1) Proceed with standard installation(默认即可)

3. 加载环境

source "$HOME/.cargo/env"

建议写入 ~/.bashrc 或 ~/.zshrc:

echo 'source "$HOME/.cargo/env"' >> ~/.bashrc

4. 验证

rustc --version
cargo --version

二、创建并运行第一个 Rust 项目

cargo new hello
cd hello
cargo run

输出:

Hello, world!

三、Debian 通过 apt 安装(不推荐但可用)

sudo apt install -y rustc cargo

⚠️ 缺点:

  • 版本通常较旧
  • 不利于多版本管理

四、常用配置(开发体验优化)

1. 更换国内镜像(可选,加速)

mkdir -p ~/.cargo
cat >> ~/.cargo/config.toml <<EOF
[source.crates-io]
replace-with = "rsproxy"

[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
EOF

2. 安装常用工具

cargo install cargo-edit
cargo install rustfmt
cargo install clippy

3. 安装 VSCode 插件(如用 VSCode)

  • Rust Analyzer
  • Code LLDB(调试用)

五、系统依赖注意(常见坑)

如果编译报类似:

could not find openssl

安装:

sudo apt install -y pkg-config libssl-dev

其他常见:

sudo apt install -y libclang-dev clang

六、升级与卸载

升级

rustup update

卸载

rustup self uninstall

如果你是用于:

  • 服务器运行 Rust 程序
  • 嵌入式 / WASM
  • Debian 旧版本(如 Debian 10)

可以告诉我具体场景,我可以给你更针对性的配置。

0 踩