初學 Rust,考慮了許久,計劃 Rust+Neon+Node.js+Electron 開發些東西玩一玩
不過在第一步配置環境上就踩了好多坑 😂 故分享出來……方便你們第一時間趕到現場嘲笑git
裂牆推薦 VS Code+WSL2,編輯器選擇 VS Code 無話可說,至於環境爲啥推薦 WSL2,緣由以下:github
關於以上第二點,本來我也用的 WSL1,前期一直很順利,用 GDB 調試也很正常,不過換成 LLDB 以後死活啓動不了調試器,遂斗膽換成了WSL2,而後嘛……真香json
雖然不知道自試用 WSL2 兩個月以來發生了什麼,可是目前的 WSL2 各方面感受都蠻優秀了curl
直接參考官方教程就好了,不過可能須要臨時換個源來加速安裝:編輯器
# 鏡像加速,使用的是中科大的源,速度很快 export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup # 若是執意使用 WSL1,則可能須要設置這個 export RUSTUP_IO_THREADS=1 # 安裝命令,後續選項通常默認就行了 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # 添加到 path source $HOME/.cargo/env # 我使用的是 fish 須要特別設置 set -U fish_user_paths ~/.cargo/bin $fish_user_paths # 更改 crates.io 爲清華源,這裏貌似要比中科大好一些,能夠自行測試 cat > ~/.cargo/config [source.crates-io] registry = "https://github.com/rust-lang/crates.io-index" replace-with = "ustc" [source.ustc] registry = "git://mirrors.ustc.edu.cn/crates.io-index" # <Ctrl+D>
更改工具鏈的源,添加以下內容到 ~/.cargo/config
文件中工具
[source.crates-io] registry = "https://github.com/rust-lang/crates.io-index" # 替換成任一鏡像源 replace-with = 'tuna' # replace-with = 'ustc' # replace-with = 'sjtu' # replace-with = 'rustcc' # 清華大學 [source.tuna] registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git" # 中國科學技術大學 [source.ustc] registry = "git://mirrors.ustc.edu.cn/crates.io-index" # 上海交通大學 [source.sjtu] registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index" # rustcc社區 [source.rustcc] registry = "git://crates.rustcc.cn/crates.io-index"
須要注意的是,儘可能不要直接修改 ~/.cargo/config
文件,可能會由於換行符致使 cargo 沒法讀取……可使用以下命令導入測試
cat >> ~/.cargo/config # 這裏粘貼你的配置文件 [source.crates-io] registry = "https://github.com/rust-lang/crates.io-index" replace-with = 'ustc' [source.ustc] registry = "git://mirrors.ustc.edu.cn/crates.io-index" # 按下 Ctrl+D 結束
在 VS Code - WSL2 中安裝以下擴展:url
關於 CodeLLDB,若是使用 WSL1 或者不想用 LLDB 調試的話,能夠安裝 C/C++ 擴展,而後使用 GDB 進行調試。spa
新建個項目看下效果:.net
注意:第一次打開 .rs 文件時,VS Code 右下角會提示安裝工具鏈,確認安裝就行了,這一步最好已配置好國內源,否則會慢死……
而後 運行 → 打開配置 CodeLLDB 會檢測到 .toml 文件,自動生成 launch.json 配置,加點兒代碼,放個斷點,最後 F5 調試,大功告成!