温馨提示×

怎样备Debian Rust环境

小樊
42
2026-09-11 03:47:39
栏目: 编程语言

下面按从零开始的思路,给你一份在 Debian 上准备 Rust 开发环境 的实用指南,涵盖安装、工具链、常用组件和排错。


一、系统准备(推荐)

sudo apt update
sudo apt upgrade -y

建议安装基础编译工具(Rust 某些 crate 需要 C 编译器):

sudo apt install -y build-essential pkg-config libssl-dev curl git

二、安装 Rust(官方推荐方式)

✅ 使用 rustup(最推荐)

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

按提示选择:

  • 默认:1) Proceed with standard installation

安装完成后:

source "$HOME/.cargo/env"

或重新打开终端。

验证:

rustc --version
cargo --version

三、配置国内镜像(可选但强烈推荐)

1️⃣ Cargo 镜像(crates.io)

编辑 ~/.cargo/config.toml

[source.crates-io]
replace-with = "rsproxy"

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

[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"

或使用 ustc / tuna 镜像也可。


2️⃣ rustup 镜像(下载工具链)

export RUSTUP_DIST_SERVER=https://rsproxy.cn
export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup

可写入 ~/.bashrc~/.zshrc


四、常用 Rust 工具安装

rustup component add rustfmt clippy
  • rustfmt:代码格式化
  • clippy:代码静态检查

可选:

cargo install cargo-edit
cargo install cargo-watch

五、创建第一个项目

cargo new hello_rust
cd hello_rust
cargo run

六、Debian 上常见问题

1️⃣ OpenSSL 相关错误

确保安装:

sudo apt install libssl-dev pkg-config

2️⃣ 静态编译(服务器常用)

sudo apt install musl-tools
rustup target add x86_64-unknown-linux-musl
cargo build --target x86_64-unknown-linux-musl

3️⃣ 多版本管理

rustup toolchain install stable
rustup toolchain install nightly
rustup default stable

七、推荐开发方式

  • 编辑器:VS Code + rust-analyzer
  • 终端:可直接用 cargo
  • 容器(进阶):Debian + Rust 官方镜像

八、最小环境总结

apt install build-essential curl git libssl-dev pkg-config
curl https://sh.rustup.rs -sSf | sh
rustup component add rustfmt clippy

如果你告诉我:

  • Debian 版本(11 / 12 / 13)
  • 是否用于 Web / 系统 / 嵌入式 / 服务器
  • 是否使用 Docker / WSL

我可以给你更精确的环境方案。

0