Rust最新版安装(v1.78.0+)

  • 系统: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代码

在这里插入图片描述

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值