(歷經200多次修改 ,只為更好的內容)
目錄:
0.試用rust
- 安裝Rust
- 安裝VScode編輯器及語言服務器
- 安裝調試工具和插件
- 建立項目并進行調試設置
- 附錄:rustlings的使用
〇、試用Rust
如果你只是想試試Rust的語法,請點擊(手機也可以)
Hello World - 通過例子學 Rust?rustwiki.org
打開后如下,代碼塊中的代碼可以修改,點擊代碼塊右上角三角形圖標運行。
一、安裝Rust
- 下載Rust安裝工具
- 使用鏡像加速Rust安裝
- 安裝Rust(兩種工具鏈可供選擇)
- 安裝標準庫源碼
- 使用鏡像加速Rust包下載
- 設置Rust包環境變量
1.下載rustup-init.exe(Rust安裝工具)
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工具鏈
- gnu(本文使用MinGW-w64)占用空間?。?00M),對于初步的rust使用足夠了。
- msvc(本文使用Visual Studio Build Tools,包含msvc,clang等)占用空間大(2G)。
已經安裝了Visual Studio的選擇msvc,已經有MinGW-w64或者msys2,或者不想占用太多空間的使用gnu。
更多區別見這個討論
GCC/G++、ICC、Clang、MSVC、BCC等C/C++編譯器有什么特長和不足??www.zhihu.com[圖片上傳失敗...(image-13f825-1602307315539)]
gnu依次輸入(注意每行需回車執行):
- 2
- x86_64-pc-windows-gnu
- nightly(想用最新的功能就用nightly(每夜版),也可以選擇stable(穩定版))
- 一路回車即可,直到看到正在下載中(有kb/s的字樣)
msvc依次輸入:
- 2
- x86_64-pc-windows-msvc
- nightly(想用最新的功能就用nightly(每夜版),也可以選擇stable(穩定版))
- 一路回車即可,直到看到正在下載中(有kb/s的字樣)
這里只是安裝了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;">cargo_path =
path +=
path,"User")
</pre>
二、安裝VScode編輯器及語言服務器
- 安裝VSCode(當前最流行的代碼編輯器)
- 安裝語言服務器(有兩種可供選擇)
- 安裝人工智能補全插件TabNine
1??. 安裝VSCode
https://http://code.visualstudio.com/?code.visualstudio.com
2??. 安裝語言服務器
有2種選擇,推薦rust-analyzer
- rls(rust language server)安裝簡單(新手推薦,安裝后還是可以再安裝rust-analyzer)
- rust-analyzer(官方維護的rls 2.0)安裝會多幾個步驟(需要安裝node.js,需要下載語言服務器,需要在vscode的setting.json中進行設置)
可以參考
PrivateRookie:VSCode 使用 rust-analyzer?zhuanlan.zhihu.com
Rust Analyzer:一款旨在帶來優秀IDE體驗的編譯器?cloud.tencent.com(一)路線:rls
- 安裝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即可
(二)路線: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插件
- a. 移動 rust-analyzer-windows.exe
把前面下載的rust-analyzer-windows.exe放到"C:/Users/你的用戶名/.cargo/bin/文件夾下(注意:替換你的用戶名。)
- b. 安裝rust-analyzer插件
點擊下面的鏈接,打開網頁后點擊install按鈕,這會打開vscode,然后install即可
rust-analyzer - Visual Studio Marketplace?marketplace.visualstudio.com
安裝后出現:沒有找到語言服務器,是否下載?點擊Cancel(下一步手動設置)
[圖片上傳失敗...(image-ea5ea8-1602307315541)]
- c. 插件設置語言服務器路徑
vscode中按下F1,輸入open settings,選擇圖中無Default字樣的
在setting.json最后一行添加語言服務器的路徑,注意替換[你的用戶名]
<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的人工智能補全。
三、安裝調試工具和插件
如果你不進行斷點調試(或不知道它),只需要安裝調試工具,不需要安裝插件。
- gnu調試工具和插件
- msvc調試工具和插件
根據安裝rust時的選項,gnu和msvc工具鏈二選一。
(一)路線:gnu工具鏈
這里我們選擇簡單的mingw-w64,還可以選擇msys2等。
1. 安裝MinGW-w64
- 下載MinGW-w64
點擊下載MinGW-w64?nchc.dl.sourceforge.net
- 設置選項(64位)
[圖片上傳失敗...(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;">mingw_path = ls
mingw_path =
path +=
path, "User")
</pre>
我們要用的是MinGW-w64中的gdb調試工具
32位或詳細設置請參考此文:C語言關注:MinGW-w64安裝教程——著名C/C++編譯器GCC的Windows版本
2. 安裝插件Native Debug(gnu工具鏈)
安裝步驟同rust-analyzer插件
(二)路線:msvc工具鏈
- 下載安裝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+),但是安裝插件之后下載它的依賴項時非常慢,網速快的人可以試試。
四、建立項目并進行調試設置
- 創建項目并打開
- 激活TabNine對Rust的補全
- 進行項目的調試設置(使用斷點調試)
- 試驗調試功能(使用斷點調試)
- F5一鍵運行
1 .創建項目并在VSCode里打開
比如hello_world,以測試調試功能
- 在vscode中按下 **Ctrl + ` **(鍵盤Tab上面的鍵)打開powershel
- 切換到你想創建項目的目錄(比如D:)
- 使用cargo創建項目(否則vscode會提示缺少Cargo.toml文件)
<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>
- 使用Open Folder按鈕打開你的hello_world目錄
- 單擊列表中的main.rs文件,打開它
第1部分操作https://www.zhihu.com/video/1180629229510811648
2. 激活TabNine對Rust的補全
TabNine的人工智能補全,需要在相應編程語言的文件里編輯模式下輸入TabNine::sem打開
[圖片上傳失敗...(image-28513-1602307315541)]
這樣.rs中文件輸入后,會打開Rust的人工智能補全
如果影響性能,可以輸入TabNine::nosem關閉
如果你使用的是rust-analyzer,TabNine還會自動安裝rls和racer等
3. 進行項目的調試設置(不使用斷點調試請跳過):
- 打開調試窗口(左側昆蟲圖標)
- 點擊設置圖標(齒輪)
- 選擇彈出的GDB(msvc為)
- 修改launch.json中的target項(msvc為program項),并添加 preLaunchTask項
a. 路線:gnu
- 選擇彈出的GDB
- 修改launch.json中的target項,并添加 preLaunchTask項
<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
- 選擇彈出的c++ windows
- 修改launch.json中的program項,并添加 preLaunchTask項
<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)
- 修改tasks.json中的label項和command項為
<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
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
Tip:VSCode調試時,把鼠標挪動到變量上面,可以顯示當前變量值。圖中把鼠標移動到實例self上,懸浮窗顯示了self的值
[圖片上傳失敗...(image-f5aba-1602307315540)]
想要不編譯直接運行時,在當前目錄下中運行
cargo run
命令即可
可以使用rust的gdb_rust_pretty_printing,在調試中enums, slices, vectors等結構會更好地顯示,但也有人覺得在windows上使用這個功能不好,有時會導致長時間的暫?;蚩ㄋ谰唧w參考how-to-set-up-gdb-for-debugging-rust-programs-in-windows
5. F5一鍵運行
如果你感覺每次都去點切換debug窗口比較麻煩,或者不需要斷點調試,請參照此部分設置。
如果你還沒有tasks.json文件,請在命令面板(Ctrl+Shift+P或F1)輸入
configure task
,選擇修改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,出現下圖
找到圖中的Tasks:Rerun Last Task,給它設置快捷鍵F5。具體方法是雙擊該條目,出現提示框后按下F5,然后再按回車。
注意:這會覆蓋自帶的F5的編譯命令,如果你需要使用斷點調試,也可以使用其他鍵代替比如Ctrl+F5
在該條目上點右鍵,選擇Change When Expression
復制粘貼editorTextFocus && editorLangId == 'rust'
這樣就只有rust語言中按下F5才是這個功能。
同樣,把Tasks:Run Task設置為Ctrl+F5(上面如果rerun task為Ctrl+F5,這里就改成其他鍵),同樣需要修改when只在Rust中運行。
我們再來說說這個功能怎么用。打開main.rs,然后按下Ctrl+F5,會出現run, build,release,check,test(我們定義的task+項目名),選擇按下回車確認就行了。
- run 是直接運行
- build是編譯
- check 是編譯,但不生成可執行文件,比build速度快(用來檢查是否有錯誤)
- release是按最高性能編譯(比build速度慢,用來發布使用,平時沒有必要使用)
- test是測試
關于build和release的性能詳細區別,見這篇文章
霧色:Rust中的零成本抽象(二)(部分翻譯)?zhuanlan.zhihu.com[圖片上傳失敗...(image-b2b800-1602307315539)]
這樣控制臺會自動運行你選擇的命令,vscode沒有關閉前,下次按下F5會運行上次命令,不再需要選擇。這樣我們能快速的運行該命令查看輸出,不需要手動切換到debug界面。
如果你安裝的是rust-analyzer,它附帶一個命令
rust-analyzer:run
,也可以在命令面板(Ctrl+Shift + P)運行或者設置快捷鍵:
它會顯示提示窗口,并運行當前位置的二進制或者測試. 在運行重復的單個測試時非常有用,請把這個綁定到快捷鍵!
五、附錄:rustlings的使用(強烈推薦)
- 安裝rustlings
- 使用rustlings
- 根據編譯錯誤修改文件以通關
- 總結與Tip
rustlings是官方的教學游戲,它的玩法類似解密游戲。通過解決程序中的錯誤通過關卡。
它作為和文檔、rust-by-example三足鼎力的新手入門教程,確實讓人感覺到好玩、驚艷。
- 安裝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 env:TMP/install_rustlings.ps1; Unblock-File
env: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的題目順序為,請先看過教程再來答題(中文教程見文末鏈接)
- 變量
- if條件
- 函數
- 原生類型
- 結構體
- 枚舉
- 模塊
- 宏
- 引用和借用
- 錯誤處理、option和result
- 標準庫
- 其他
使用答題功能,需要在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
流程就是:
- 看文檔
- 再看example
- 再刷rustlings通關
注意:在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