前言
前几日,我在新系统上重新部署 Rust 开发环境,又折腾了一番,特此记录。
前置环境
因为不想碰太臃肿的 MSVC ,但是 Rust requires a linker and Windows API libraries
,因此本文采用 GNU 工具链代替 MSVC。
- 本文默认已通过 MSYS2 安装
mingw-w64
,并正确配置gcc
等环境变量。 - 若尚未安装,请先参考 官方文档 或 本博客 MSYS2 安装教程 完成该步骤。
前置准备
下载对应版本的 rustup-init.exe。
rustup & cargo 安装路径
如需自定义安装路径,请预先设置以下环境变量:
变量名 | 变量值(默认) |
---|---|
RUSTUP_HOME | C:\Users\User\.rustup |
CARGO_HOME | C:\Users\User\.cargo |
配置 rustup 下载镜像
更改下载镜像可以大幅提升安装文件下载速度(毕竟文件服务器在海外,速度不稳定是很正常的)。
添加环境变量,变量名为:
RUSTUP_DIST_SERVER
变量值为(示例,使用清华源):
https://mirrors.tuna.tsinghua.edu.cn/rustup
安装 rustup
双击 rustup-init.exe
安装,在跳出的控制台窗口中一般会显示:
Rust Visual C++ prerequisites
Rust requires a linker and Windows API libraries but they don't seem to be
available.
These components can be acquired through a Visual Studio installer.
1) Quick install via the Visual Studio Community installer
(free for individuals, academic uses, and open source).
2) Manually install the prerequisites
(for enterprise and advanced users).
3) Don't install the prerequisites
(if you're targeting the GNU ABI).
输入3
并回车,即跳过 MSVC 安装。
随后显示:
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:
C:\Users\Administrator\.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
C:\Users\Administrator\.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:
C:\Users\Administrator\.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
确认安装路径无误后,直接回车(选择默认选项 1)。安装完成后将看到:
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload its PATH environment variable to include
Cargo's bin directory (%USERPROFILE%\.cargo\bin).
Press the Enter key to continue.
此时 rustup 安装完成。
打开新的 CMD,运行:
rustc --version
若输出版本号,说明安装成功。
更改 cargo 镜像源
在 %CARGO_HOME%
目录下新建或编辑 config.toml
,内容如下(参考自:https://www.cnblogs.com/-CO-/p/18041169):
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
# 具体使用的源
replace-with = 'tuna'
## 配置多个源地址
# rsproxy
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
# 阿里源
[source.aliyun]
registry = "sparse+https://mirrors.aliyun.com/crates.io-index/"
# 清华大学
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
# 中国科学技术大学
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index/"
# 上海交通大学
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"
# rustcc社区
[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"
[net]
git-fetch-with-cli=true
配置工具链
- 查看可用工具链:
rustup target list
- 切换到 GNU 工具链:
rustup default stable-x86_64-pc-windows-gnu
至此,Rust + GNU 环境配置完毕。