前言:为什么选择 Rust?国内安装为何如此重要?
Rust 以其内存安全、零成本抽象、并发无惧 的特性,正成为系统编程、Web 后端、区块链、嵌入式开发的首选语言。
被 Google、微软、Meta、亚马逊、华为等大厂采用,Linux 内核也接纳 Rust,Rust正在以它的方式改变着世界。
但国内开发者常因网络问题,在安装 rustup
(Rust 安装工具)和下载 cargo
包时卡顿、失败。
本文将为你提供一份专为国内网络环境优化的完整安装指南,涵盖:
- ✅ 使用国内镜像源快速安装 Rust
- ✅ 配置 Cargo 源加速包下载
- ✅ VSCode 安装 Rust 插件与智能提示
- ✅ 编写并运行你的第一个 Rust 程序 ------
Hello, World!
无需翻墙,无需等待,10分钟内,让你的 Rust 开发环境飞起来!
✅ 第一步:安装 Rust 编译器(使用国内镜像)
❌ 传统方式(国内易失败)
bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
⚠️ 此链接位于境外,国内访问慢、超时率高,极易中断。
✅ 推荐方式:使用中国科学技术大学镜像源(稳定高速)
1. 下载并运行官方安装脚本(替换为中科大镜像)
首先需要配置2个rust的环境变量


然后去去阿里云镜像网站下载rustup
选择rustup点进去,选择archive,再选择一个版本
选择win 的 gnu,如果你选择的是llvm 会比较复杂需求去下载微软的visual studio之类的庞然大物,对u小白非常不友好。当然也可以再后面点击rust.exe的时候切换为gnu。
很快你就会下载好一个叫rust.exe的软件,双击它

2. 按提示选择安装配置

输入 2
可以通过如下命令下载gnu
rustup toolchain install stable-x86_64-pc-windows-gnu
执行后,就会用最开始配置的镜像地址去下载rust相关的内容,终端会提示:
vbnet
Welcome to Rust!
This will download and install the official compiler for the Rust programming language, and its package manager, Cargo.
It will add the cargo, rustc, rustup and other commands to Cargo's bin directory, located at:
C:\Users\YourName\.cargo\bin
This path will then be added to your PATH environment variable by modifying the HKEY_CURRENT_USER/Environment/PATH registry key.
You can uninstall at any time with rustup self uninstall and these changes will be reverted.
Current installation options:
default host triple: x86_64-pc-windows-msvc
default toolchain: stable
profile: default
modify PATH variable: yes
1) Proceed with standard installation (default)
2) Customize installation
3) Cancel installation
✅ 直接按回车选择 1(默认安装)即可。安装过程会自动下载工具链,全程使用国内镜像,速度极快!
3. 验证安装
安装完成后,关闭并重新打开终端(重要!),然后输入:
bash
rustc --version
cargo --version
你应该看到类似输出:

✅ 出现版本号,恭喜你,Rust 环境已成功安装!
✅ 第二步:配置 Cargo 源(加速包下载)
Rust 的包管理器 cargo
默认从 crates.io
下载第三方库(crate),国内访问极慢。
✅ 方法:使用清华大学开源软件镜像站
1. 创建或编辑 Cargo 配置文件
在你的用户主目录下创建 .cargo
文件夹(如 Windows 是 C:\Users\你的用户名\.cargo
,Linux/macOS 是 /home/你的用户名/.cargo
),然后在里面创建文件 config
(无后缀)。
2. 编辑 config
文件内容
用文本编辑器(如 VSCode)打开 .cargo/config
,粘贴以下内容:
toml
[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
✅ 说明:
source.crates-io
是默认源,我们用replace-with
替换它source.ustc
指向清华大学镜像,地址是git://
协议,比https://
更稳定
3. 验证配置(可选)
你可以尝试下载一个包测试:
bash
cargo new hello_cargo
cd hello_cargo
cargo build
首次构建会下载依赖,如果速度很快(几秒完成),说明镜像生效!
💡 清华镜像支持
git://
协议,比https://
更快更稳定,是目前国内最佳选择。
✅ 第三步:在 VSCode 中配置 Rust 开发环境
1. 安装 VSCode(如未安装)
前往官网下载:code.visualstudio.com/
国内用户可从清华源下载:mirrors.tuna.tsinghua.edu.cn/electron/(搜索 VSCode
)
这里也可以使用基于vscode的AI IDE,如trae,cursor等。
2. 安装 Rust 插件
打开 VSCode,点击左侧扩展图标(或按 Ctrl+Shift+X
),搜索并安装以下两个插件:
插件名称 | 作者 | 功能 |
---|---|---|
rust-analyzer | rust-analyzer | 🔥 核心插件 :提供智能补全、跳转、重构、错误提示(推荐取代旧版 rust 插件) |
CodeLLDB | vadimcn | 调试器支持(用于断点调试) |
✅ 强烈建议 :安装完
rust-analyzer
后,重启 VSCode。
3. 配置 rust-analyzer(推荐)
打开 VSCode 设置(Ctrl+,
),搜索 rust-analyzer
,确保以下选项启用:
- ✅
Rust-analyzer > Enable
------ 开启 - ✅
Rust-analyzer > Check On Save
------ 开启(保存时自动检查) - ✅
Rust-analyzer > Cargo Run Build Script
------ 开启(构建时自动运行 build script)
⚠️ 如果你同时安装了旧的
rust
插件(by rust-lang),请禁用或卸载它,避免冲突!
✅ 第四步:编写并运行你的第一个 Rust 程序 ------ Hello World!
1. 创建项目
在终端中执行:
bash
cargo new hello_world
cd hello_world
cargo new
会自动创建一个标准的 Rust 项目结构:
bash
hello_world/
├── Cargo.toml # 项目配置文件(类似 package.json)
└── src/
└── main.rs # 主程序入口
2. 编写代码
用 VSCode 打开 hello_world
文件夹,打开 src/main.rs
,内容默认如下:
rust
fn main() {
println!("Hello, world!");
}
✅ 这就是 Rust 的 Hello World!非常简洁。
3. 运行程序
在终端中(确保在 hello_world
目录下),输入:
bash
cargo run
你会看到输出:
scss
Compiling hello_world v0.1.0 (D:\hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 0.85s
Running `target\debug\hello_world.exe`
Hello, world!
✅ 恭喜你! 你成功用 Rust 编写了第一个程序!
4. 在 VSCode 中直接运行
- 在
main.rs
文件中右键,选择 "Run Code" - 或按
Ctrl+Shift+P
→ 输入Rust Analyzer: Run
→ 回车
VSCode 会自动调用 cargo run
,并在内置终端中显示结果。
✅ 第五步:进阶建议(让开发更丝滑)
建议 | 说明 |
---|---|
✅ 安装 rustfmt 格式化工具 |
cargo install rustfmt ,VSCode 保存时自动格式化代码 |
✅ 安装 clippy 代码检查工具 |
cargo install clippy ,帮助你写出更规范、更高效的 Rust 代码 |
✅ 学习官方文档 | doc.rust-lang.org/book/ 中文版:kaisery.github.io/trpl-zh-cn/ |
📊 国内安装流程总结(一句话记住)
镜像源 + 配置文件 + rust-analyzer = 国内 Rust 开发无障碍!
步骤 | 操作 | 作用 |
---|---|---|
1 | 设置 RUSTUP_DIST_SERVER 和 RUSTUP_UPDATE_ROOT 为中科大镜像 |
加速 Rust 编译器下载 |
2 | 创建 .cargo/config 并配置清华 crates.io 镜像 |
加速第三方库下载 |
3 | 安装 VSCode + rust-analyzer + CodeLLDB | 提供智能编码与调试体验 |
4 | cargo new hello_world → cargo run |
验证环境,运行程序 |
💬 结语:Rust,值得你花时间
Rust 不是"下一个 JavaScript",它是未来十年系统级开发的基石语言 。
它教会你如何写出安全、高效、可信赖的代码------这在 AI、区块链、操作系统、WebAssembly 领域至关重要。
别再被网络问题劝退了!
按照本文步骤,10分钟内,你就能在国产网络环境下,用 Rust 写出你的第一个程序。
✅ 今天你迈出的这一步,就是你技术生涯的转折点。
📚 推荐资源
类型 | 链接 |
---|---|
Rust 官方中文文档 | kaisery.github.io/trpl-zh-cn/ |
中国科学技术大学镜像站 | mirrors.ustc.edu.cn/ |
清华大学开源软件镜像站 | mirrors.tuna.tsinghua.edu.cn/ |
rust-analyzer 官方文档 | rust-analyzer.github.io/ |
VSCode Rust 插件市场 | marketplace.visualstudio.com/items?itemN... |