温馨提示×

ubuntu rust环境怎么配置

小樊
43
2026-10-28 06:39:46
栏目: 编程语言

在 Ubuntu 上配置 Rust 开发环境,一般按下面步骤来即可,适用于 Ubuntu 20.04 / 22.04 / 24.04。


一、安装 Rust(推荐方式)

1. 更新系统

sudo apt update
sudo apt upgrade -y

2. 安装依赖

sudo apt install -y build-essential curl git

3. 使用官方脚本安装 Rust

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

安装过程中会让你选择:

  • 默认直接回车(推荐)

4. 加载环境变量

source $HOME/.cargo/env

或者重新打开终端。

5. 验证安装

rustc --version
cargo --version

二、配置 Cargo 镜像(国内加速,可选)

编辑配置文件:

mkdir -p ~/.cargo
nano ~/.cargo/config.toml

示例(USTC 镜像):

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

[source.ustc]
registry = "sparse+https://mirrors.ustc.edu.cn/crates.io-index/"

保存后退出。


三、编写第一个 Rust 程序

cargo new hello
cd hello
cargo run

输出:

Hello, world!

四、常用工具(推荐安装)

1. 代码格式化

rustup component add rustfmt

2. 静态检查

rustup component add clippy

使用:

cargo fmt
cargo clippy

五、VS Code 配置(推荐)

sudo snap install code --classic

安装扩展:

  • Rust Analyzer
  • Code LLDB(调试用)

六、更新与卸载

更新 Rust

rustup update

卸载 Rust

rustup self uninstall

如果你是用在 WSL / Docker / 服务器 / 嵌入式 环境,或者想装 特定 Rust 版本 / nightly,可以告诉我,我可以给你更具体的配置方案。

0 踩