debian

Debian下Rust开发环境如何配置

小樊
50
2025-10-02 06:59:15
栏目: 编程语言

Debian下配置Rust开发环境的详细步骤

1. 更新系统包列表

在开始安装前,确保系统包列表是最新的,避免依赖冲突:

sudo apt update && sudo apt upgrade -y

2. 安装必要依赖

Rust编译及工具链需要build-essential(包含gcc、make等基础编译工具)、curl(下载rustup)等依赖:

sudo apt install curl build-essential gcc make -y

3. 使用rustup安装Rust

rustup是Rust官方推荐的工具链管理工具,可灵活管理Rust版本及组件。执行以下命令安装:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --no-modify-path -y

4. 配置环境变量

为使Rust命令(rustccargo)全局可用,需将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  # 立即生效

5. 验证安装

通过以下命令检查Rust及Cargo(Rust包管理器)是否安装成功:

rustc --version  # 查看Rust编译器版本
cargo --version  # 查看Cargo版本

若输出类似rustc 1.75.0 (x86_64-unknown-linux-gnu)的版本信息,则说明安装成功。

6. (可选)安装开发辅助工具

rustup component add rustfmt clippy

7. (可选)配置国内镜像源

为加速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

8. (可选)创建并运行示例项目

使用cargo快速创建一个“Hello World”项目,验证开发环境:

cargo new hello_world  # 创建项目目录
cd hello_world         # 进入项目目录
cargo run            # 编译并运行

若终端输出Hello, world!,则说明项目运行正常。

9. (可选)使用VS Code增强开发体验

通过以上步骤,你可在Debian系统上搭建一个完整的Rust开发环境,满足日常开发需求。如需升级Rust版本,可使用rustup update命令。

0
看了该问题的人还看了