【Rust敲门砖】 Windows环境下配置及安装环境

一、安装C++环境

rust底层是依赖C环境的连接器,所以需要先安装C/C++编译环境, 有两种选择:安装微软的msvc或者安装mingw/cygwin

如果使用msvc的Visual Studio,只需要安装好C/C++编译环境,然后一路默认就行了,缺点是体积比较大,下载安装都要好几个G,参见:安装MSVC。

本文主要讲解mingw-64的环境下的安装操作,看看下载页面 https://www.mingw-w64.org/downloads/

可以看到有很多种mingw-64的构建方式,支持windows的也不少,我本人目前用的是上图中圆圈标记的那个。

Mingw-builds 的构建版本

地址为:https://github.com/niXman/mingw-builds-binaries/releases

如果你是64位的系统,那就选择x86_64开头的,反之32位的选择i686开头的。

线程模型有posixwin32,如果你想在vscode里面搞断点调试,我推荐用posix。

然后是异常处理机制,我目前用的seh

● seh 零开销exception,64位系统可用

● sjlj 不是零成本,有较小的性能损失

● dwarf 只支持32位

我选择的是x86_64-12.2.0-release-posix-seh-ucrt-rt_v10-rev2.7z,然后下载到本地,把bin文件夹加到Path系统环境变量里面即可,这个适合老手,毕竟这个压缩包才70Mb左右,比安装msvc那一套快,又省空间。

至此,C/C++环境就搞定了,就是这么简单。

bash 复制代码
gcc -v

二、安装rust环境

安装之前,先想想要安装到什么地方,我不喜欢安装到C盘,可以设置一些系统环境变量,改变默认安装的位置

复制代码
RUSTUP_HOME	D:\Programs\rust\.rustup  // 默认路径 ~/.rustup 或 %USERPROFILE%/.rustup
CARGO_HOME	D:\Programs\rust\.cargo // 默认路径 ~/.cargo 或 %USERPROFILE%/.cargo

如果直接从官方网站下载,国内的环境复杂,下载会很慢,而且容易失败。推荐使用镜像加速安装,设置以下环境变量:

复制代码
RUSTUP_DIST_SERVER	https://rsproxy.cn
RUSTUP_UPDATE_ROOT	https://rsproxy.cn/rustup

这一切准备工作做好之后,就可以用rustup-init来安装了

如果使用msvc环境的话,一切默认就行了

而mingw就需要手动选择gnu toolchain

复制代码
msvc的toolchain:  stable-x86_64-pc-windows-msvc
mingw/cygwin:     stable-x86_64-pc-windows-gnu

运行rustup-init后出现如下提示:

它说rust需要windows API库和链接器,你可以选择下面3项的一项。

1、安装Visual Studio。

2、手动安装的预设条件。

3、无需预设条件。

我们这里选择3,mingw-w64也是C/C++编译器,就是 GCC 的 Windows 版本 。回车:

复制代码
 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,(小提示,你先复制x86_64-pc-windows-gnu,在rustup右键可以直接粘贴文字,可以不用手写输入。出现提示:

复制代码
Default toolchain? (stable/beta/nightly/none) [stable]

输入stable,表示稳定版。出现提示:

复制代码
Profile (which tools and data to install)? (minimal/default/complete) [default]

输入default,表示默认。出现提示:

复制代码
Modify PATH variable? (Y/n)

输入Y,表示修改环境变量,然后回到了第一步,提示:

复制代码
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,然后出现:

bash 复制代码
info: profile set to 'default'
info: setting default host triple to x86_64-pc-windows-gnu
info: syncing channel updates for 'stable-x86_64-pc-windows-gnu'
info: latest update on 2022-12-15, rust version 1.66.0 (69f9c33d7 2022-12-12)
info: downloading component 'cargo'
  7.3 MiB /   7.3 MiB (100 %)   1.9 MiB/s in  5s ETA:  0s
info: downloading component 'clippy'
  3.6 MiB /   3.6 MiB (100 %)   1.7 MiB/s in  2s ETA:  0s
info: downloading component 'rust-docs'
 19.0 MiB /  19.0 MiB (100 %)   2.1 MiB/s in 10s ETA:  0s
info: downloading component 'rust-mingw'
  4.2 MiB /   4.2 MiB (100 %)   2.6 MiB/s in  1s ETA:  0s
info: downloading component 'rust-std'
 27.8 MiB /  27.8 MiB (100 %)   1.8 MiB/s in 14s ETA:  0s
info: downloading component 'rustc'
 74.3 MiB /  74.3 MiB (100 %)   1.5 MiB/s in 49s ETA:  0s
info: downloading component 'rustfmt'
  6.6 MiB /   6.6 MiB (100 %)   2.2 MiB/s in  3s ETA:  0s
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 19.0 MiB /  19.0 MiB (100 %)   3.0 MiB/s in  5s ETA:  0s
info: installing component 'rust-mingw'
info: installing component 'rust-std'
 27.8 MiB /  27.8 MiB (100 %)   9.0 MiB/s in  3s ETA:  0s
info: installing component 'rustc'
 74.3 MiB /  74.3 MiB (100 %)  10.3 MiB/s in  7s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-pc-windows-gnu'

  stable-x86_64-pc-windows-gnu installed - rustc 1.66.0 (69f9c33d7 2022-12-12)

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 (D:\Programs\rust\cargo_home\bin).

Press the Enter key to continue.

等待它下载文件完毕,这里需要等待的时间有点长,但耐心就好,假如下载出现错误,就重设上面的设定,再来一次流程。

当下载文件完毕后,rustup可能自动关闭。

打开命令提示符,也就是cmd。

输入rustc -V,记得是大写的V。

出现相关信息后,标明安装rust成功了。

复制代码
rustup show 
bash 复制代码
#安装其他类型的toolchain开发环境
rustup toolchain install <toolchain>

#例如
rustup toolchain install  stable-x86_64-pc-windows-gnu

#切换默认的toolchain
rustup default [toolchain]

#例如
rustup default stable-x86_64-pc-windows-gnu

#更新:
rustup update stable
rustup default stable

#用Cargo运行如下命令新建工程:
PS D:\vstest\main> cargo new hello_cargo
Created binary (application) `hello_cargo` package
相关推荐
柏油5 小时前
MySQL InnoDB 行锁
数据库·后端·mysql
咖啡调调。5 小时前
使用Django框架表单
后端·python·django
白泽talk5 小时前
2个小时1w字| React & Golang 全栈微服务实战
前端·后端·微服务
摆烂工程师5 小时前
全网最详细的5分钟快速申请一个国际 “edu教育邮箱” 的保姆级教程!
前端·后端·程序员
一只叫煤球的猫6 小时前
你真的会用 return 吗?—— 11个值得借鉴的 return 写法
java·后端·代码规范
Asthenia04126 小时前
HTTP调用超时与重试问题分析
后端
颇有几分姿色6 小时前
Spring Boot 读取配置文件的几种方式
java·spring boot·后端
AntBlack6 小时前
别说了别说了 ,Trae 已经在不停优化迭代了
前端·人工智能·后端
@淡 定6 小时前
Spring Boot 的配置加载顺序
java·spring boot·后端