rust语言

安装Rust及VSCode环境设置(Windows 10)

2020-10-10  本文已影响0人  科幻经典

(历经200多次修改 ,只为更好的内容)

DCjanusCrLF0710的帮助让这一篇教程成为可能,在此致以真挚地感谢

目录:

0.试用rust

  1. 安装Rust
  2. 安装VScode编辑器及语言服务器
  3. 安装调试工具和插件
  4. 建立项目并进行调试设置
  5. 附录:rustlings的使用

〇、试用Rust

如果你只是想试试Rust的语法,请点击(手机也可以)

Hello World - 通过例子学 Rust​rustwiki.org

打开后如下,代码块中的代码可以修改,点击代码块右上角三角形图标运行。

image

一、安装Rust

  1. 下载Rust安装工具
  2. 使用镜像加速Rust安装
  3. 安装Rust(两种工具链可供选择)
  4. 安装标准库源码
  5. 使用镜像加速Rust包下载
  6. 设置Rust包环境变量

1.下载rustup-init.exe(Rust安装工具)

点击下载rustup-init​win.rustup.rs

2. 使用镜像加速rustup安装

打开Powershell(Win+X +A)执行(执行完不要关闭,后面还会多次用到):

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">[environment]::SetEnvironmentvariable("RUSTUP_DIST_SERVER", "https://mirrors.ustc.edu.cn/rust-static"
, "User")
[environment]::SetEnvironmentvariable("RUSTUP_UPDATE_ROOT", "https://mirrors.ustc.edu.cn/rust-static/ rustup", "User")

</pre>

参考链接:https://http://lug.ustc.edu.cn/wiki/mirrors/help/rust-static

3. 安装Rust:打开rustup-init.exe

如果出现安装程序未经微软验证,点击install anyway(立即安装)即可。

[图片上传失败...(image-ec24b7-1602307315541)]

安装有2种选择,gnu或者msvc工具链

已经安装了Visual Studio的选择msvc,已经有MinGW-w64或者msys2,或者不想占用太多空间的使用gnu。

更多区别见这个讨论

GCC/G++、ICC、Clang、MSVC、BCC等C/C++编译器有什么特长和不足?​www.zhihu.com[图片上传失败...(image-13f825-1602307315539)]

gnu依次输入(注意每行需回车执行):

msvc依次输入:

这里只是安装了Rust,这两种工具链到第三部分再下载安装。

Rust卸载:

rustup self uninstall

打开Rust本地文档:

rustup doc

Rust中文文档地址:Rust 程序设计语言 简体中文版

4. 安装标准库源码

语言服务器需要源码,打开powershell执行:

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">

 rustup component add rust-src 

</pre>

5. 使用镜像加速cargo包下载:

打开Powershell执行:

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">

"[source.crates-io]
registry = 'https://github.com/rust-lang/crates.io-index'
replace-with = 'ustc'
[source.ustc]
registry = 'https://mirrors.ustc.edu.cn/crates.io-index/'
"|Out-File -Encoding utf8 $home\.cargo\config

</pre>

脚本在用户目录.cargo目录下新建了文件config,用中科大镜像加快cargo包下载。
cargo类似于npm,是 Rust 的构建系统和包管理器。
参考链接:https://http://lug.ustc.edu.cn/wiki/mirrors/help/rust-crates

6. 设置cargo环境变量(以后的rustup可能会自动设置)

打开Powershell执行:

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">path = [environment]::GetEnvironmentvariable("Path","User")cargo_path = env:USERPROFILE + "\.cargo\bin"path += cargo_path + ";" [environment]::SetEnvironmentvariable("Path",path,"User")

</pre>


二、安装VScode编辑器及语言服务器

  1. 安装VSCode(当前最流行的代码编辑器)
  2. 安装语言服务器(有两种可供选择)
  3. 安装人工智能补全插件TabNine

1️⃣. 安装VSCode

https://http://code.visualstudio.com/​code.visualstudio.com

2️⃣. 安装语言服务器

有2种选择,推荐rust-analyzer

可以参考

PrivateRookie:VSCode 使用 rust-analyzer​zhuanlan.zhihu.com

图标 Rust Analyzer:一款旨在带来优秀IDE体验的编译器​cloud.tencent.com

(一)路线:rls

  1. 安装rls

安装语言服务器,执行

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">

rustup component add rls rust-analysis 

</pre>

2.安装Rust(rls)插件

点击下面的链接,打开网页后点击install按钮,这会打开vscode,然后install即可

https://http://marketplace.visualstudio.com/items?itemName=rust-lang.rust​marketplace.visualstudio.com

(二)路线:rust-analyzer

1. 安装node.js

后续的rust-analyzer需要安装node.js,下载地址:

https://http://nodejs.org/dist/v12.16.0/node-v12.16.0-x64.msi​nodejs.org

2. 下载rust-analyzer

点击链接下载语言服务器rust-analyzer-windows.exe

rust-analyzer release​github.com

下载速度慢的话,可以使用2020年5月11日的版本(使用百度网盘下载),然后再慢慢下载新版本

链接: https://http://pan.baidu.com/s/1jY0qhtxu0lSrerdpCSdDiw 提取码: v3py

安装和使用说明还可以参考(推荐完整完成后观看,有很多有用的功能,文末也有链接):
https://http://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/features.md
PrivateRookie:VSCode 使用 rust-analyzer

3. 安装rust-analyzer插件

把前面下载的rust-analyzer-windows.exe放到"C:/Users/你的用户名/.cargo/bin/文件夹下(注意:替换你的用户名。)

点击下面的链接,打开网页后点击install按钮,这会打开vscode,然后install即可

rust-analyzer - Visual Studio Marketplace​marketplace.visualstudio.com

图标

安装后出现:没有找到语言服务器,是否下载?点击Cancel(下一步手动设置)

[图片上传失败...(image-ea5ea8-1602307315541)]

vscode中按下F1,输入open settings,选择图中无Default字样的

image

在setting.json最后一行添加语言服务器的路径,注意替换[你的用户名]

image

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">"rust-analyzer.serverPath": "C:/Users/你的用户名/.cargo/bin/rust-analyzer-windows.exe",</pre>

3️⃣. 安装TabNine插件(rls和rust-analyzer都推荐安装)

这是一个人工智能补全工具,会给出很多代码补全提示,很多时候只需要一直Tab

TabNine - Visual Studio Marketplace​marketplace.visualstudio.com

图标

后面四.2部分会讲到如何激活Rust的人工智能补全。


三、安装调试工具和插件

如果你不进行断点调试(或不知道它),只需要安装调试工具,不需要安装插件。

  1. gnu调试工具和插件
  2. msvc调试工具和插件

根据安装rust时的选项,gnu和msvc工具链二选一。

(一)路线:gnu工具链

这里我们选择简单的mingw-w64,还可以选择msys2等。

1. 安装MinGW-w64

点击下载MinGW-w64​nchc.dl.sourceforge.net

[图片上传失败...(image-f3515f-1602307315541)]

使用powershell设置环境变量

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">path = [environment]::GetEnvironmentvariable("Path", "User")mingw_path = ls env:programfiles\mingw-w64\*\mingw64\binmingw_path = mingw_path.toString()path += mingw_path + ";" [environment]::SetEnvironmentvariable("Path",path, "User")

</pre>

我们要用的是MinGW-w64中的gdb调试工具
32位或详细设置请参考此文:C语言关注:MinGW-w64安装教程——著名C/C++编译器GCC的Windows版本

2. 安装插件Native Debug(gnu工具链)

安装步骤同rust-analyzer插件

https://http://marketplace.visualstudio.com/items?itemName=webfreak.debug​marketplace.visualstudio.com

(二)路线:msvc工具链

  1. 下载安装Visual Studio 2019 生成工具(build tools)

Thank you for downloading Visual Studio - Visual Studio​visualstudio.microsoft.com

运行后,选择c++ 生成工具,安装即可

[图片上传失败...(image-f43d56-1602307315541)]

如果你已经安装了Visual Studio,就无需安装build tools,

直接在工具——获取工具和功能中,勾选c++桌面开发,安装即可

[图片上传失败...(image-8dd0fd-1602307315541)]

2. 安装插件C/C++

C/C++ - Visual Studio Marketplace​marketplace.visualstudio.com

图标

还有一个CodeLLDB插件,安装之后就不需要安装上面两类工具链和插件(需要python3.5+),但是安装插件之后下载它的依赖项时非常慢,网速快的人可以试试。


四、建立项目并进行调试设置

  1. 创建项目并打开
  2. 激活TabNine对Rust的补全
  3. 进行项目的调试设置(使用断点调试)
  4. 试验调试功能(使用断点调试)
  5. F5一键运行

1 .创建项目并在VSCode里打开

比如hello_world,以测试调试功能

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">cd d:
cargo new hello_world

</pre>

第1部分操作https://www.zhihu.com/video/1180629229510811648

image

2. 激活TabNine对Rust的补全

TabNine的人工智能补全,需要在相应编程语言的文件里编辑模式下输入TabNine::sem打开

[图片上传失败...(image-28513-1602307315541)]

这样.rs中文件输入后,会打开Rust的人工智能补全

如果影响性能,可以输入TabNine::nosem关闭

如果你使用的是rust-analyzer,TabNine还会自动安装rls和racer等

3. 进行项目的调试设置(不使用断点调试请跳过)

a. 路线:gnu

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">"target": "./target/debug/${workspaceFolderBasename}.exe",
"preLaunchTask": "build",
</pre>

b. 路线:msvc

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">"program": "./target/debug/${workspaceFolderBasename}.exe",
"preLaunchTask": "build",
</pre>

两种路线都需要VSCode自动生成tasks.json(选择从template生成—>others)

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">"label": "build",
"command": "cargo build",</pre>

第2部分操作https://www.zhihu.com/video/1180776456249159680

image

4. 修改main.rs的内容,并在每行前点击出红点(断点),以方便观察调试功能(不使用断点调试请跳过)

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">fn main() { let mut a = 0; a = 1; a = 5; a = 7; println!("Hello, world!"); } </pre>

第3部分操作https://www.zhihu.com/video/1180631885650321408

image

Tip:VSCode调试时,把鼠标挪动到变量上面,可以显示当前变量值。图中把鼠标移动到实例self上,悬浮窗显示了self的值

[图片上传失败...(image-f5aba-1602307315540)]

想要不编译直接运行时,在当前目录下中运行

cargo run

命令即可
可以使用rust的gdb_rust_pretty_printing,在调试中enums, slices, vectors等结构会更好地显示,但也有人觉得在windows上使用这个功能不好,有时会导致长时间的暂停或卡死具体参考how-to-set-up-gdb-for-debugging-rust-programs-in-windows

5. F5一键运行

如果你感觉每次都去点切换debug窗口比较麻烦,或者不需要断点调试,请参照此部分设置。

如果你还没有tasks.json文件,请在命令面板(Ctrl+Shift+P或F1)输入

configure task

,选择 image

修改tasks.json内容为(也可以增加其他task,运行时根据label(标签)进行区分)

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "run",
"type": "shell",
"command": "cargo run",
"problemMatcher": []
},
{
"label": "build",
"type": "shell",
"command": "cargo build",
"problemMatcher": []
},
{
"label": "release",
"type": "shell",
"command": "cargo build --release",
"problemMatcher": []
},
{
"label": "check",
"type": "shell",
"command": "cargo check",
"problemMatcher": []
},
{
"label": "test",
"type": "shell",
"command": "cargo test",
"problemMatcher": []
},
]
}</pre>

打开快捷键设置(Keyboard Shortcuts)

[图片上传失败...(image-95d4de-1602307315540)]

输入task,出现下图

image

找到图中的Tasks:Rerun Last Task,给它设置快捷键F5。具体方法是双击该条目,出现提示框后按下F5,然后再按回车。

注意:这会覆盖自带的F5的编译命令,如果你需要使用断点调试,也可以使用其他键代替比如Ctrl+F5

在该条目上点右键,选择Change When Expression

image

复制粘贴editorTextFocus && editorLangId == 'rust'

image

这样就只有rust语言中按下F5才是这个功能。

同样,把Tasks:Run Task设置为Ctrl+F5(上面如果rerun task为Ctrl+F5,这里就改成其他键),同样需要修改when只在Rust中运行。

我们再来说说这个功能怎么用。打开main.rs,然后按下Ctrl+F5,会出现run, build,release,check,test(我们定义的task+项目名),选择按下回车确认就行了。

关于build和release的性能详细区别,见这篇文章

雾色:Rust中的零成本抽象(二)(部分翻译)​zhuanlan.zhihu.com[图片上传失败...(image-b2b800-1602307315539)]

这样控制台会自动运行你选择的命令,vscode没有关闭前,下次按下F5会运行上次命令,不再需要选择。这样我们能快速的运行该命令查看输出,不需要手动切换到debug界面。

如果你安装的是rust-analyzer,它附带一个命令

rust-analyzer:run

,也可以在命令面板(Ctrl+Shift + P)运行或者设置快捷键:
它会显示提示窗口,并运行当前位置的二进制或者测试. 在运行重复的单个测试时非常有用,请把这个绑定到快捷键!


五、附录:rustlings的使用(强烈推荐)

  1. 安装rustlings
  2. 使用rustlings
  3. 根据编译错误修改文件以通关
  4. 总结与Tip

rustlings是官方的教学游戏,它的玩法类似解密游戏。通过解决程序中的错误通过关卡。

它作为和文档、rust-by-example三足鼎力的新手入门教程,确实让人感觉到好玩、惊艳。

  1. 安装rustlings

在powershell中执行,选择y

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">

set-executionpolicy remotesigned

</pre>

接着执行(注意cd到你想放项目的文件夹)

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">cd home/.rustup/ Invoke-WebRequest https://git.io/rustlings-win | Select-Object -ExpandProperty Content | Out-Fileenv:TMP/install_rustlings.ps1; Unblock-File env:TMP/install_rustlings.ps1; Invoke-Expressionenv:TMP/install_rustlings.ps1

</pre>

如果失败的话,请手动下载安装(先cd到你要存放rustlings的位置)

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">git clone https://github.com/rust-lang/rustlings
cd rustlings
git checkout tags/2.1.0 # or whatever the latest version is (find out at https://github.com/rust-lang/rustlings/releases/latest)
cargo install --force --path .

</pre>

如果有安装错误,确保工具链是最新的:

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">

rustup update

</pre>

2. 使用rustlings

rustlings的题目顺序为,请先看过教程再来答题(中文教程见文末链接)

  1. 变量
  2. if条件
  3. 函数
  4. 原生类型
  5. 结构体
  6. 枚举
  7. 模块
  8. 引用和借用
  9. 错误处理、option和result
  10. 标准库
  11. 其他

使用答题功能,需要在rustlings目录下执行

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">

rustlings watch

</pre>

建议在vscode中打开这个文件夹,然后每次在powershell中运行。

[图片上传失败...(image-2bd5be-1602307315539)]

3. 根据弹出的编译错误修改exercises中的文件

rustlings会根据概念难易的顺序逐个编译文件,当编译错误的时候,你需要去修正该错误文件并保存。

注意:不要关闭rustlings watch运行的窗口,它会自动检测你修改保存的文件

修改完成之后,如果要跳至下一关,则需要把当前关文件中的该行注释删除保存。

<pre style="margin: 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px;">//I AM NOT DONE </pre>

4. 流程总结与Tip

流程就是:

注意:在exercises文件夹的每个分类中,都有一个README,里面有当前分类对应的rust-by-example的地址,推荐先看再做题(网页是英文的,可以自己打开中文网页,见文末)
Rust中文文档地址:Rust 程序设计语言 简体中文版
提示:弹出编译错误时,在VSCode中按住Ctrl点击该文件名,会直接打开该文件

[图片上传失败...(image-378ebc-1602307315539)]

如有错误,请不吝告知!

觉得好的话,请点赞收藏!让知乎呈现更多你喜欢的东西。

如果你是初学者的话,建议把本网页添加到浏览器书签(Ctrl+ D),可以经常浏览下面的常用链接。


常用链接:

英文文档:

rustup doc

Rust 程序设计语言 中文版(深圳加速站)​120.78.128.153Rust By Example 中文版​rustwiki.orgCargo - Cargo 中文文档​cargo.budshome.comrust-analyzer features​github.comPrivateRookie:VSCode 使用 rust-analyzer​zhuanlan.zhihu.com

图标

参考文章:

DCjanus:【新手向】从零开始配置Windows下Rust开发环境图解​zhuanlan.zhihu.com

图标 CrLF0710:Rust 环境配置事项一览​zhuanlan.zhihu.com[图片上传失败...(image-b04dca-1602307315539)] Win7 VSCode 离线安装Rust语言及环境配置​www.cnblogs.com 图标 Matrix:Rust和Windows不得不说的事儿​zhuanlan.zhihu.com[图片上传失败...(image-7f6583-1602307315539)] 草头黄:喜欢Rust的理由,今天发现了一个。用手机上网也可以编程。编程,编译,运行,使用依赖库,都能简单化。​zhuanlan.zhihu.com[图片上传失败...(image-f11597-1602307315539)]
上一篇下一篇

猜你喜欢

热点阅读