前提条件
在 Windows 上安装 Rust 需要使用 C++ 工具
所以需要先安装配置好 C++ 环境,推荐使用MinGW64或者MSYS2来部署。
查看 C++ 版本:
C:\Users\sheng>g++ --version
g++ (Rev2, Built by MSYS2 project) 12.1.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
设置 Rust 环境变量(非必要)
注意:这一步一定是要在安装前设置。
设置 rustup 路径安装路径与 cargo 安装路径,Rust 默认安装在 C 盘, 这里把安装路径换到 D 盘。
设置 Rust 的下载更新服务器源为清华大学镜像源
以管理员身份运行终端(cmd或者powershell),执行一下命令:
setx RUSTUP_HOME "D:\Rust\rustup" /m
setx CARGO_HOME "D:\Rust\cargo" /m
setx RUSTUP_UPDATE_ROOT "https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup" /m
setx RUSTUP_DIST_SERVER "https://mirrors.tuna.tsinghua.edu.cn/rustup" /m
打开系统环境变量表验证是否全部添加和是否添加正确。
重启系统使环境变量生效
重启后打开终端输入:echo %RUSTUP_HOME% 可看到添加的环境变量
下载 Rust 安装程序
下载地址:https://www.rust-lang.org/learn/get-started
安装 Rust
直接运行安装程序
因为我不是使用vs构建的c++,所以选择3
下面是加载安装目录
到达安装位置的时候,选择2,自定义安装
把x86_64-pc-windows-msvc修改成x86_64-pc-windows-gnu,使用MinGW64或者MSYS2工具链构建的C++都是用gnu
D:\Downloads>.\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 targetting the GNU ABI).
>3
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
>2
I'm going to ask you the value of each of these installation options.
You may simply press the Enter key to leave unchanged.
Default host triple? [x86_64-pc-windows-msvc]
x86_64-pc-windows-gnu
Default toolchain? (stable/beta/nightly/none) [stable]
Profile (which tools and data to install)? (minimal/default/complete) [default]
Modify PATH variable? (Y/n)
y
Current installation options:
default host triple: x86_64-pc-windows-gnu
default toolchain: stable
profile: default
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>
验证安装成功:
新打开一个cmd,在里面输入 rustc --version 则会输出版本信息。显示没有命令的可以重新手动添加一下path然后重启一下机子。
安装 Visual Studio Code
通过使用 Visual Studio Code (VS Code) 作为文本编辑器/集成开发环境 (IDE),可以利用诸如代码完成、语法突出显示、格式设置和调试等语言服务。
下载地址:https://code.visualstudio.com/
下载直接安装
安装之后需要安装一些扩展:
Better TOML: 使用 toml 做项目的配置管理
Code Runner:运行代码
CodeLLDB:调试代码
Rust Syntax:提供语法高亮
rust-analyzer:语法检测
在VS Code 中运行Rust 的 Hello, world! 教程
在rust 工作目录(没有固定,自己自定义在哪)中,使用cargo 创建一个rust项目。
然后进入目录,使用code 打开
PS D:\Rust\src> cargo new first_rust_project
Created binary (application) `first_rust_project` package
PS D:\Rust\src> cd .\first_rust_project\
PS D:\Rust\src\first_rust_project> code .
在vs code 资源管理器中,打开 src
>main.rs
文件。内容如下:
fn main() {
println!("Hello, world!");
}
方法一运行程序:
点击右上角的运行按钮
输出如下:
[Running] cd "d:\Rust\src\first_rust_project\src\" && rustc main.rs && "d:\Rust\src\first_rust_project\src\"main
Hello, world!
[Done] exited with code=0 in 1.065 seconds
方法二运行程序:
在 VS Code 中使用 Ctrl + Shift + ` 打开一个终端,在d:\Rust\src\first_rust_project 目录下,执行 cargo run
输出如下:
PS D:\Rust\src\first_rust_project> cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target\debug\first_rust_project.exe`
Hello, world!
就这样,程序运行没问题,环境搭建完成。