Windows下安装Rust语言编程环境

系统要求

  • Windows 10 或更高版本。必须为 64 位。
  • 至少 8GB 的内存。
  • 至少 4GB 的 C 盘空间,以及 6GB 的其他盘空间。

设置环境变量

添加如下环境变量:

变量名
CARGO_HOME
cargo 的安装路径(必须为绝对路径),例如D:\Rust\Cargo
RUSTUP_HOMErustup 的安装路径(必须为绝对路径),例如 D:\Rust\Rustup
RUSTUP_DIST_SERVER
https://mirrors.ustc.edu.cn/rust-static
RUSTUP_UPDATE_ROOThttps://mirrors.ustc.edu.cn/rust-static/rustup
RUST_BACKTRACEfull

提示

  • 安装路径不能出现汉字或空格。
  • 中科大镜像是国内最快的开源镜像站之一,当然也可以依据个人喜好换成其他源。

下载所需文件

记住它所在的目录! 如下图中的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

  1. Proceed with installation (default)
  2. Customize installation
  3. 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.cn/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。
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值