使用 vscode 开发 Rust
本文将介绍如果基于 vscode 搭建 Rust 开发环境。
rust 安装
虽然是介绍 IDE 的搭建,还是顺带介绍下 Rust 的安装。默认官方推荐是使用 rustup 安装:
1 | curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh |
- 已安装的 rust 可以使用
rustup update更新。 - 在 Rust 开发环境中,所有工具都安装在
~/.cargo/bin目录中。 - 可以通过运行
rustup self uninstall
rust proxy
在国内会有网络问题,建议使用 rust 代理 安装。
- 步骤一:设置 Rustup 镜像, 修改配置
~/.zshrcor~/.bashrc
1 | export RUSTUP_DIST_SERVER="https://rsproxy.cn" |
- 步骤二:安装 Rust
1 | curl --proto '=https' --tlsv1.2 -sSf https://rsproxy.cn/rustup-init.sh | sh |
- 步骤三:设置 crates.io 镜像,修改配置
~/.cargo/config.toml
1 | [source.crates-io] |
vscode 配置
安装插件
环境问题
Rust build异常: linker cc not found
1 | # linux |
rust-analyzer
自定义 Cargo 路径
rust analyzer 有时会遇到页面上run 执行报错:shell 中 找不到 Cargo。可以在用户配置中增加:
1 | { |
然后重启 rust analyzer server。
CodeLLDB
CodeLLDB 可以帮助 vscode 使用 LLDB 调试 Rust 代码。
- Macos 自带 LLDB,但是版本较老,可以使用
brew install lldb安装最新的 lldb。 - Ubuntu 可以使用
sudo apt -y install lldb安装 lldb。
CodeLLDB 支持使用 Use Alternate Backend... 命令 或者 lldb.library 配置修改系统默认的 LLDB。配置格式如下:
- Linux:
<lldb root>/lib/liblldb.so.<verson>。 - MacOS:
<lldb root>/lib/liblldb.<version>.dylib - Windows:
<lldb root>/bin/liblldb.dll
<lldb root>是 LLDB安装的根目录