環境
- Windows 10
下載 rust-init
進入官網的下載頁面:https://www.rust-lang.org/tools/install
根據 32 還是 64 位系統選擇下載不同的 exe 文件,下載完成后雙擊運行。
安裝 Rust
前置條件說明
默認情況,Rust 依賴 C++ build tools
,所以有一個提醒,並且提供了安裝的方式。
Rust Visual C++ prerequisites
Rust requires the Microsoft C++ build tools for Visual Studio 2013 or later, but they don't seem to be installed.
The easiest way to acquire the build tools is by installing Microsoft Visual C++ Build Tools 2019 which provides just the Visual C++ build tools:
https://visualstudio.microsoft.com/visual-cpp-build-tools/
Please ensure the Windows 10 SDK and the English language pack components are included when installing the Visual C++ Build Tools.
Alternately, you can install Visual Studio 2019, Visual Studio 2017, Visual Studio 2015, or Visual Studio 2013 and during install select the "C++ tools":
https://visualstudio.microsoft.com/downloads/
Install the C++ build tools before proceeding.
If you will be targeting the GNU ABI or otherwise know what you are doing then it is fine to continue installation without the build tools, but otherwise, install the C++ build tools before proceeding.
Continue? (y/N) y
默認安裝
安裝繼續時,先是說明了會安裝哪些工具,並且會安裝到哪個目錄,可以通過哪些方式修改,這里都直接默認。
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\jiangbo\.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory located at:
C:\Users\jiangbo\.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\jiangbo\.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
>1
驗證安裝成功
使用命令 rustc --version
命令驗證是否安裝成功。
C:\Users\jiangbo>rustc --version
rustc 1.54.0 (a178d0322 2021-07-26)
解決鏈接錯誤
linker link.exe
not found
如果這時候直接新建項目運行,則會得到一個錯誤提示,完整的錯誤提示見附錄。
下載生成工具
訪問 https://visualstudio.microsoft.com/zh-hans/visual-cpp-build-tools/ 點擊下載生成工具。
下載完成后雙擊安裝,然后確定下一步。
安裝生成工具
安裝完成后,提示重啟計算機,其實不重啟也不影響使用 Rust 的編譯。
驗證運行
附錄
error: linker link.exe
not found
解決方式參考上面的解決鏈接錯誤
> Executing task: C:\Users\JiangBo\.cargo\bin\cargo.exe run --package hello_world --bin hello_world <
Compiling hello_world v0.1.0 (D:\workspace\rust\hello_world)
error: linker `link.exe` not found
|
= note: 系統找不到指定的文件。 (os error 2)
note: the msvc targets depend on the msvc linker but `link.exe` was not found
note: please ensure that VS 2013, VS 2015, VS 2017 or VS 2019 was installed with the Visual C++ option
error: aborting due to previous error
error: could not compile `hello_world`
To learn more, run the command again with --verbose.
The terminal process "C:\Users\JiangBo\.cargo\bin\cargo.exe 'run', '--package', 'hello_world', '--bin', 'hello_world'" terminated with exit code: 101.
Terminal will be reused by tasks, press any key to close it.