VSCode Extensions for Rust
To make Rust programming easier and more efficient in VSCode, consider installing the following extensions:
- Rust Analyzer: This is an alternative to the Rust (rls) extension. It’s a powerful tool that provides a range of features, including type inference, auto-completion, and real-time error checking.
- CodeLLDB: A LLDB extension for Visual Studio Code, which is especially useful for debugging Rust code.
- TOML Language Support: Offers syntax highlighting and other features for TOML files, which are used by Rust for configuration.
To install any extension, follow these steps:
- Open VSCode.
- Click on the Extensions view icon on the sidebar or press
Ctrl+Shift+X
. - Search for the extension you want to install.
- Click Install.
Installing Rust and Its Build Dependencies
Here’s a quick guide on how to install Rust and its build dependencies on Linux, Windows, and macOS.
Linux
- Open Terminal.
- Download and install rustup by running:
1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- Source the cargo environment:
1
source $HOME/.cargo/env
Windows
- Open Terminal.
- Download and install rustup by running:
1
2
winget install Rustlang.Rustup
rustup toolchain install stable-x86_64-pc-windows-msvc
- You might need to add the Rust path to your environment.
macOS
- Open Terminal
- Download and install rustup by running:
1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- Source the cargo environment:
1
source $HOME/.cargo/env
Create a new project with Cargo
1
2
3
cargo new <your-project-name>
# Or with an existing directory
# cargo init
Setting Up Debugging in VSCode
After installing the CodeLLDB extension, you need to set up your launch.json and tasks.json files to enable debugging.
launch.json
Create a new file in your .vscode
directory and name it launch.json
. Copy and paste the following configuration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Windows",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/<your-project-name>.exe",
"preLaunchTask": "rust: cargo build",
"args": [
],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "internalConsole",
},
{
"name": "Debug Linux",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/target/debug/<your-project-name>",
"preLaunchTask": "rust: cargo build",
"args": [
],
"cwd": "${fileDirname}",
"console": "internalConsole",
"stopOnEntry": false,
"sourceLanguages": ["rust"],
},
]
}
Replace <your-project-name>
with your project’s name.
tasks.json
Create another new file in your .vscode
directory and name it tasks.json
. Copy and paste the following configuration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"version": "2.0.0",
"tasks": [
{
"type": "cargo",
"command": "build",
"problemMatcher": [
"$rustc"
],
"group": "build",
"label": "rust: cargo build"
}
]
}
Now, you’re all set! You can run your program by pressing F5
, and VSCode will build and debug your Rust application.
Sources
- Rust Analyzer, https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer (last visited May 26, 2023).
- CodeLLDB, https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb (last visited May 26, 2023).
- Other Installation Methods - Rust Forge, https://forge.rust-lang.org/infra/other-installation-methods.html (last visited May 26, 2023).