在 WSL2 上配置 Rust 环境
安装 Rust 工具链
在终端直接运行以下命令:
1
| curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
|
检查安装是否成功
1 2 3 4 5
| rustc -V rustc 1.79.0 (129f3b996 2024-06-10)
cargo -V cargo 1.79.0 (ffa9cf99a 2024-06-03)
|
WSL 环境下如果没有此信息,请关闭并重新打开终端,再次执行以上命令。
Cargo 是 Rust 的项目管理工具。
VSCode 插件推荐
Rust-Analyzer
CodeLLDB
Error Lens(更直观的错误提示)
Even Better TOML(支持 .toml 文件完整特性)
Project Manager(收藏、识别和管理你的工作空间和项目文件夹)
更新 / 卸载 Rust
修改下载镜像为国内的镜像地址
在 $HOME/.cargo/config.toml (如果文件不存在则手动创建一个)中添加以下内容:
1 2 3 4 5 6 7 8
| [registries] ustc = { index = "https://mirrors.ustc.edu.cn/crates.io-index/" }
[source.ustc] registry = "sparse+https://mirrors.ustc.edu.cn/crates.io-index/"
[source.crates-io] replace-with = 'ustc'
|
Rust-Analyzer 格式化配置
setting.json 添加配置如下。
1 2 3 4 5 6 7
| "[rust]": { "editor.formatOnSave": true, }, "rust-analyzer.rustfmt.extraArgs": [ "--config", "hard_tabs=false,tab_spaces=4", ],
|
一些 Rust 资料
Rust圣经