- 系统:Windows 11 专业版 23H2
- rustc:1.78.0
配置环境变量和设置配置文件
新建文件夹“C:\Rust\Rustup”和“C:\Rust\Cargo”。【以管理员身份运行】打开CMD
设置系统环境变量,如下设置RUSTUP_DIST_SERVER,其余同理
C:\Windows\System32>setx RUSTUP_DIST_SERVER http://mirrors.ustc.edu.cn/rust-static
成功: 指定的值已得到保存。
setx RUSTUP_DIST_SERVER http://mirrors.ustc.edu.cn/rust-static
setx RUSTUP_UPDATE_ROOT http://mirrors.ustc.edu.cn/rust-static/rustup
setx RUSTUP_HOME C:\Rust\Rustup
setx CARGO_HOME C:\Rust\Cargo
【以管理员身份运行】重新打开CMD,用set命令查看环境变量配置情况。
C:\Windows\System32>set
...
CARGO_HOME=C:\Rust\Cargo
...
RUSTUP_DIST_SERVER=http://mirrors.ustc.edu.cn/rust-static
RUSTUP_HOME=C:\Rust\Rustup
RUSTUP_UPDATE_ROOT=http://mirrors.ustc.edu.cn/rust-static/rustup
...
新建文件"C:\Rust\Cargo\config",config无需后缀
config文件内容如下
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
# 指定镜像
replace-with = 'ustc' # 如:tuna、sjtu、ustc,或者 rustcc
# 注:以下源配置一个即可,无需全部
# 中国科学技术大学
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"
# 上海交通大学
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index/"
# 清华大学
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
# rustcc社区
[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"
开始安装rustup-init
下载“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 targeting the GNU ABI).
>1
info: downloading Visual Studio installer
3.8 MiB / 3.8 MiB (100 %) 867.7 KiB/s in 7s ETA: 0s
info: running the Visual Studio install
info: rustup will continue once Visual Studio installation is complete
安装程序会自动下载Visual Studio installer,【继续】
全部勾选,【安装】
【关闭】
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:\Rust\Rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
C:\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:
C:\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 standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
>1
info: profile set to 'default'
info: default host triple is x86_64-pc-windows-msvc
info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'
info: latest update on 2024-05-02, rust version 1.78.0 (9b00956e5 2024-04-29)
info: downloading component 'cargo'
6.2 MiB / 6.2 MiB (100 %) 1.8 MiB/s in 3s ETA: 0s
info: downloading component 'clippy'
2.1 MiB / 2.1 MiB (100 %) 1.6 MiB/s in 1s ETA: 0s
info: downloading component 'rust-docs'
15.1 MiB / 15.1 MiB (100 %) 1.7 MiB/s in 8s ETA: 0s
info: downloading component 'rust-std'
18.0 MiB / 18.0 MiB (100 %) 1.7 MiB/s in 10s ETA: 0s
info: downloading component 'rustc'
57.4 MiB / 57.4 MiB (100 %) 1.7 MiB/s in 34s ETA: 0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
15.1 MiB / 15.1 MiB (100 %) 1.6 MiB/s in 14s ETA: 0s
info: installing component 'rust-std'
18.0 MiB / 18.0 MiB (100 %) 13.4 MiB/s in 1s ETA: 0s
info: installing component 'rustc'
57.4 MiB / 57.4 MiB (100 %) 16.9 MiB/s in 3s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-pc-windows-msvc'
stable-x86_64-pc-windows-msvc installed - rustc 1.78.0 (9b00956e5 2024-04-29)
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload its PATH environment variable to include
Cargo's bin directory (C:\Rust\Cargo\bin).
Press the Enter key to continue.
验证安装
安装完成,文件夹“C:\Rust\Rustup”和“C:\Rust\Cargo”新增很多文件。CMD运行:
C:\Users\sywq6>rustc -V
rustc 1.78.0 (9b00956e5 2024-04-29)
使用Visual Studio Code开发调试
创建新项目
下载VS Code,下载地址为:Download Visual Studio Code - Mac, Linux, Windows
新建文件夹rust_demo,打开这个文件夹,新建终端,新建项目
cargo new rust_demo
修改main.rs
fn main() {
let number = 3;
if number < 5 {
println!("condition was true");
} else {
println!("condition was false");
}
}
运行项目
cd .\rust_demo\
cargo run
调试代码
打开Visual Studio Code插件市场,下载三个插件
- rust-analyzer
- CodeLLDB
- Code Runner
设置断点,点击【Debug】,开始调试Rust代码