admin管理员组文章数量:1571366
目录
- 系统要求
- 设置环境变量
- 提示
- 下载所需文件
- 安装 VS 2022 生成工具
- 安装 Rustup
- 配置 Cargo
- 验证安装正确性
- 可能会出现错误
- 解决方法
系统要求
- Windows 10 或更高版本。必须为 64 位。
- 至少 8GB 的内存。
- 至少 4GB 的 C 盘空间,以及 6GB 的其他盘空间。
设置环境变量
添加如下环境变量:
变量名 | 值 |
---|---|
CARGO_HOME | cargo 的安装路径(必须为绝对路径),例如D:\Rust\Cargo |
RUSTUP_HOME | rustup 的安装路径(必须为绝对路径),例如 D:\Rust\Rustup |
RUSTUP_DIST_SERVER | https://mirrors.ustc.edu/rust-static |
RUSTUP_UPDATE_ROOT | https://mirrors.ustc.edu/rust-static/rustup |
RUST_BACKTRACE | full |
提示
- 安装路径不能出现汉字或空格。
- 中科大镜像是国内最快的开源镜像站之一,当然也可以依据个人喜好换成其他源。
下载所需文件
- Visual Studio 2022 生成工具(已经安装了 VS 2022 的可以不下载此文件)。
- rustup-init.exe(一定要下载 64 位的)
记住它所在的目录! 如下图中的rustup-init.exe
下载位置为D:\T
:
安装 VS 2022 生成工具
(已经安装了 VS 2022 的可以跳过此步骤)
以管理员身份运行 VS 2022 Build Tools 的安装程序。
点击【单个组件】,勾选【MSVC v143 - VS 2022 C++ x64/x86 生成工具(最新)】和 Windows 10 SDK 的最新版本:
点击【安装位置】后出现如下界面。您可以根据个人喜好修改下图中的三个目录(可以出现空格,但不建议用中文),并取消勾选【安装后保留下载缓存】。确认无误后点击【安装】:
出现如下图所示的画面,即为安装成功:
安装 Rustup
以管理员身份运行 cmd.exe
,使用 cd /d
命令进入刚才下载 rustup-init.exe 的目录,然后运行这个程序:
cd /d d:\t
rustup-init
程序输出以下信息:
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:\Rust\RustUp
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
D:\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:\Rust\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 (default)
profile: default
modify PATH variable: yes
- Proceed with installation (default)
- Customize installation
- Cancel installation
需要关注上面加粗的文本,表示 rustup 和 cargo 的安装位置。
若不是您想要的位置,请修改上文提到的环境变量。修改后,需要重新打开【x64 Native Tools Command Prompt for VS 2022】并重新运行rustup-init.exe
。
确认无误后,按回车。
耐心等待安装过程。若输出如下信息,表示安装成功:
配置 Cargo
在 %CARGO_HOME%
所指向的文件夹(笔者的目录为D:\Rust\Cargo
)下新建名为 config.toml
的文件,内容如下:
[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "sparse+https://mirrors.ustc.edu/crates.io-index/"
保存该文件,整个 Rust 的安装过程到此结束。
验证安装正确性
让我们创建一个 Rust 语言的“Hello World”项目并运行它。
打开命令提示符或 PowerShell,先使用cd
命令进入一个你喜欢的目录,然后使用以下命令创建项目并运行:
cargo new hello
cd hello
cargo r
若输出以下内容,表示 Rust 编译器可以正常运行:
Compiling hello v0.1.0 (D:\Projects\hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.43s
Running `target\debug\hello.exe`
Hello, world!
可能会出现错误
若发生错误,将会输出以下内容:
Compiling hello v0.1.0 (D:\Projects\hello)
error: linker `link.exe` not found
|
= note: program not found
note: the msvc targets depend on the msvc linker but `link.exe` was not found
note: please ensure that Visual Studio 2017 or later, or Build Tools for Visual Studio were installed with the Visual C++ option.
note: VS Code is a different product, and is not sufficient.
error: could not compile `hello` (bin "hello") due to previous error
上述文本的意思是:Rust 编译器依赖微软的链接器link.exe
才能运行。
解决方法
共两种解决办法:
- 以后打开【x64 Native Tools Command Prompt for VS 2022】而不是
cmd.exe
来编译运行 Rust 程序。 - 在开始菜单中打开【Visual Studio Installer】,卸载并重装 Window 10 SDK / Windows 11 SDK。
版权声明:本文标题:Windows下安装Rust语言编程环境 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1725616779a1033077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论