在开始安装前,确保系统包列表是最新的,避免依赖冲突:
sudo apt update && sudo apt upgrade -y
Rust编译及工具链需要build-essential(包含gcc、make等基础编译工具)、curl(下载rustup)等依赖:
sudo apt install curl build-essential gcc make -y
rustup是Rust官方推荐的工具链管理工具,可灵活管理Rust版本及组件。执行以下命令安装:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --no-modify-path -y
--no-modify-path:将Rust安装到/opt/rust目录(避免修改系统PATH),更符合多用户环境规范;-y:自动确认安装,无需手动交互。为使Rust命令(rustc、cargo)全局可用,需将Rust环境变量添加到系统配置中:
echo 'export RUSTUP_HOME=/opt/rust' | sudo tee -a /etc/profile.d/rust.sh
echo 'export PATH=$PATH:/opt/rust/bin' | sudo tee -a /etc/profile.d/rust.sh
source /etc/profile # 立即生效
通过以下命令检查Rust及Cargo(Rust包管理器)是否安装成功:
rustc --version # 查看Rust编译器版本
cargo --version # 查看Cargo版本
若输出类似rustc 1.75.0 (x86_64-unknown-linux-gnu)的版本信息,则说明安装成功。
rustfmt用于统一代码风格;clippy提供静态分析,发现潜在错误;lldb或gdb(Debian默认安装)。rustup component add rustfmt clippy
为加速cargo下载依赖(crates),可使用国内镜像(如清华源),编辑~/.cargo/config.toml文件:
mkdir -p ~/.cargo
cat <<EOF > ~/.cargo/config.toml
[source.crates-io]
replace-with = 'tuna'
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
EOF
使用cargo快速创建一个“Hello World”项目,验证开发环境:
cargo new hello_world # 创建项目目录
cd hello_world # 进入项目目录
cargo run # 编译并运行
若终端输出Hello, world!,则说明项目运行正常。
通过以上步骤,你可在Debian系统上搭建一个完整的Rust开发环境,满足日常开发需求。如需升级Rust版本,可使用rustup update命令。