赞
踩
添加如下环境变量:
变量名 | 值 |
---|---|
CARGO_HOME | cargo 的安装路径(必须为绝对路径),例如D:\Rust\Cargo |
RUSTUP_HOME | rustup 的安装路径(必须为绝对路径),例如 D:\Rust\Rustup |
RUSTUP_DIST_SERVER | https://mirrors.ustc.edu.cn/rust-static |
RUSTUP_UPDATE_ROOT | https://mirrors.ustc.edu.cn/rust-static/rustup |
RUST_BACKTRACE | full |
记住它所在的目录! 如下图中的rustup-init.exe
下载位置为D:\T
:
(已经安装了 VS 2022 的可以跳过此步骤)
以管理员身份运行 VS 2022 Build Tools 的安装程序。
点击【单个组件】,勾选【MSVC v143 - VS 2022 C++ x64/x86 生成工具(最新)】和 Windows 10 SDK 的最新版本:
点击【安装位置】后出现如下界面。您可以根据个人喜好修改下图中的三个目录(可以出现空格,但不建议用中文),并取消勾选【安装后保留下载缓存】。确认无误后点击【安装】:
出现如下图所示的画面,即为安装成功:
以管理员身份运行 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_HOME%
所指向的文件夹(笔者的目录为D:\Rust\Cargo
)下新建名为 config
的文件(没有扩展名),内容如下:
[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
才能运行。
共两种解决办法:
cmd.exe
来编译运行 Rust 程序。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。