这份教程不仅是安装,更是长期的环境管理指南。
—
1. 核心环境变量配置(环境的“地基”)
Rust 默认会在 C 盘创建 .rustup 和 .cargo 文件夹。要让它永远待在 D 盘,必须在系统层级锁定变量。
打开设置:按下 Win + R,输入 sysdm.cpl 并回车。
环境变量:选择 高级 选项卡 -> 点击底部的 环境变量。
新建/编辑用户变量:
检查 Path 变量:
下载安装:
在“[链接登录后可见] DOWNLOAD RUSTUP-INIT.EXE (X64),下载。
运行:rustup-init.exe安装。
info: downloading Visual Studio installer
info: running the Visual Studio install
info: rustup will continue once Visual Studio installation is complete
warn: operation completed successfully, but install requires reboot before it can be used (exit code 3010)
Welcome to Rust!
This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
D:\environment\rust\rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
D:\environment\rust\cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:
D:\environment\rust\cargo\bin
This path will then be added to your PATH environment variable by
modifying the PATH registry key at HKEY_CURRENT_USER\Environment.
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 (default)
profile: default
modify PATH variable: yes
1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
—
2. 配置国内镜像加速(“起飞”的关键)
Rust 的包管理器 Cargo 会在 CARGO_HOME 中存放下载的所有依赖包。默认服务器在海外,国内访问极慢。
前往目录:D:\environment\rust\cargo。
新建文件:新建一个名为 config 的文件(注意:没有后缀名,不要写成 config.txt)。
填入以下内容(推荐字节跳动镜像,目前最稳定):
Ini, TOML
[source.crates-io]
replace-with = 'rsproxy'
[source.rsproxy]
registry = "https://rsproxy.cn/index"
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"
[net]
git-fetch-with-cli = true
—
3. 开发环境联动(VS Code 增强)
既然你在 D 盘开发,强烈建议配合 VS Code 使用,这样你可以像写 Node.js 一样舒服地写 Rust。
在 VS Code 插件市场搜索并安装 rust-analyzer。
配置:点击该插件的设置(齿轮图标),确保它的 Server Path 能够识别到 D:\environment\rust\cargo\bin 目录下的编译器。
这会为你提供极其强大的代码补全、跳转和实时错误检查。
—
4. 验证与维护命令
环境搭建好后,你可以通过以下命令进行“体检”: