【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
相关推荐
容若只如初见2 小时前
项目实战--Spring Boot + Minio文件切片上传下载
java·spring boot·后端
码农爱java2 小时前
Spring Boot 中的监视器是什么?有什么作用?
java·spring boot·后端·面试·monitor·监视器
Apifox.3 小时前
什么是 HTTP POST 请求?初学者指南与示范
后端·http·学习方法·web
无名指的等待7123 小时前
SpringBoot实现图片添加水印(完整)
java·spring boot·后端
与墨学长4 小时前
Rust破界:前端革新与Vite重构的深度透视(中)
开发语言·前端·rust·前端框架·wasm
cong*5 小时前
Flash存储器解析:从原理到应用,全面了解其与缓存的区别
windows·经验分享·其他·计算机外设
甜甜圈的小饼干7 小时前
Spring Boot+Vue项目从零入手
vue.js·spring boot·后端
我曾遇到一束光7 小时前
Spring boot 更改启动LOGO
数据库·spring boot·后端
tiger_angel8 小时前
springboot集成gzip和zip数据压缩传输-满足2k数据自动压缩(适用大数据信息传输)
大数据·spring boot·后端·数据压缩·压缩数据·压缩传输消息
笔触狂放8 小时前
【Django】网上蛋糕项目商城-关键字搜索,商品详情功能
后端·python·django