下面按从零开始的思路,介绍在 Debian 上配置 Rust 开发环境的常见做法,覆盖安装、工具链管理、常用组件和验证。
先更新系统:
sudo apt update
sudo apt upgrade -y
安装基础依赖(编译 Rust 程序通常需要):
sudo apt install -y \
build-essential \
pkg-config \
libssl-dev \
curl \
git
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装过程中选择:
1) Proceed with standard installation(默认即可)安装完成后,加载环境变量:
source "$HOME/.cargo/env"
或重新打开终端。
rustc --version
cargo --version
示例输出:
rustc 1.81.0 (xxxxxx 2024-xx-xx)
cargo 1.81.0 (xxxxxx 2024-xx-xx)
rustup toolchain list
rustup install stable
rustup install nightly
rustup default stable
rustup update
Cargo 是 Rust 的包管理器和构建工具。
编辑或创建:
mkdir -p ~/.cargo
nano ~/.cargo/config.toml
示例(使用中国科学技术大学镜像):
[source.crates-io]
replace-with = "ustc"
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index/"
rustup component add rustfmt
rustup component add clippy
rustfmt:代码格式化clippy:静态检查使用:
cargo fmt
cargo clippy
cargo doc --open
cargo new hello_rust
cd hello_rust
cargo run
输出:
Hello, world!
rlwrap(方便 REPL 体验,可选)sudo apt install rlwrap
可后续安装 target:
rustup target add armv7-unknown-linux-gnueabihf
rustup self uninstall
sudo apt update
sudo apt install -y build-essential curl
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"
rustc --version
如果你有特定用途(如:
可以告诉我,我可以给你更针对性的配置方案。