【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++环境就搞定了,就是这么简单。

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,然后出现:

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 

在这里插入图片描述

#安装其他类型的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

相关推荐

  1. Windows环境JDK安装环境变量配置指南

    2024-02-22 06:42:03       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-22 06:42:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-22 06:42:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-22 06:42:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-22 06:42:03       20 阅读

热门阅读

  1. 加解密算法

    2024-02-22 06:42:03       31 阅读
  2. 多线程架构

    2024-02-22 06:42:03       24 阅读
  3. 学习数据结构和算法的第12天

    2024-02-22 06:42:03       28 阅读
  4. 数据挖掘-Weka的安装与使用

    2024-02-22 06:42:03       33 阅读
  5. CSS基础和选择器

    2024-02-22 06:42:03       28 阅读
  6. Flutter面试之事件分发机制

    2024-02-22 06:42:03       26 阅读