在 Debian 系统上初始化 Rust 开发环境,通常包含 安装 Rust 工具链、配置国内镜像(可选)、验证安装、创建/运行项目 几个步骤。下面按推荐流程说明。
sudo apt update
sudo apt upgrade -y
sudo apt install -y curl build-essential gcc
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装过程中选择:
1) Proceed with standard installation(默认,推荐)source $HOME/.cargo/env
或者重新打开终端。
rustc --version
cargo --version
能看到版本号说明安装成功。
编辑或创建文件:
mkdir -p ~/.cargo
nano ~/.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:
[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index/"
export RUSTUP_DIST_SERVER=https://rsproxy.cn
export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup
cargo new hello_rust
cd hello_rust
目录结构:
hello_rust
├── Cargo.toml
└── src
└── main.rs
cargo run
输出:
Hello, world!
# 更新 Rust
rustup update
# 安装特定版本
rustup install stable
# 设置默认版本
rustup default stable
# 查看工具链
rustup toolchain list
不要使用 sudo 安装 rustup
如果之前用 apt 装过旧版本,建议先卸载:
sudo apt remove rustc cargo
rustup target add x86_64-unknown-linux-musl
如果你是用于 服务器 / 嵌入式 / WASM / 系统编程,可以告诉我具体场景,我可以给你更针对性的初始化方案。