温馨提示×

ubuntu上rust怎样快速配置

小樊
43
2026-10-28 06:37:48
栏目: 编程语言

在 Ubuntu 上快速配置 Rust 开发环境,最推荐、最省事的方式是使用官方工具 rustup。下面给你一套“最快可用”的步骤。


一、快速安装 Rust(推荐方式)

1. 安装依赖

sudo apt update
sudo apt install -y curl build-essential gcc

2. 使用 rustup 安装 Rust

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

安装过程中选择:

  • ✅ 1) Proceed with standard installation(默认即可)

3. 让环境变量生效

source "$HOME/.cargo/env"

或者重新打开终端。


4. 验证安装

rustc --version
cargo --version

能看到版本号就说明成功了。


二、国内加速(可选但强烈推荐)

如果你在国内,建议配置镜像,否则下载很慢。

设置环境变量(写入 ~/.bashrc 或 ~/.zshrc)

export RUSTUP_DIST_SERVER=https://rsproxy.cn
export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup
source ~/.bashrc

Cargo 镜像(~/.cargo/config.toml)

[source.crates-io]
replace-with = 'rsproxy'

[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"

三、创建一个测试项目(验证可用)

cargo new hello
cd hello
cargo run

输出 Hello, world! 即配置完成。


四、常用命令速查

rustup update          # 更新 Rust
rustup toolchain list # 查看工具链
cargo build           # 编译
cargo run             # 运行
cargo check           # 快速检查

五、可选:安装常用组件

rustup component add rustfmt clippy

如果你是用于 嵌入式 / WASM / 服务器开发,或想用 VS Code,我也可以给你更针对性的配置方案。

0 踩