Setting up a Rust development environment with Visual Studio Code
Getting started with Rust development takes a few more steps than with other web-based programming languages.
When I decided to teach myself Rust, I naturally started looking around at what editors I could use. It turned out that most of the folks I asked online were still using Visual Studio Code for Rust. To my surprise, though, setting up a complete development environment in Visual Studio Code wasn’t as straightforward as I expected. I need to find and download different tools in order to get started.
(I’m assuming you already have Visual Studio Code installed, but if not, go install it.)
Step 1: Download and install the
The easiest starting point for most folks is to install
rustup. If you’re coming from Node.js development, the closest analog would be the
nvm in that
rustup not only installs everying that you need to start Rust development but also allows you to easily switch back and forth between release channels and install additional components.
When you install
rustup, you get a complete Rust toolchain including the compiler (
rustc) and the
cargo (closest Node.js analog is
npm). With those installed, you can start compiling Rust programs immediately.
You can read more about
rustup in the
Step 2: Create a project
Technically it’s not necessary to create a project to proceed to the next steps, but it will help you ensure that Visual Studio Code is set up correctly. So, create a simple project to get started by running:
cargo new hello-world
This command creates a new directory called
hello-world that contains a Rust project scaffold, including:
Cargo.toml- the equivalent of
src/main.rs- the Rust source file to execute
You can then
hello-world and run:
This will execute the
src/main.rs file and should print out “Hello world” if everything is installed correctly. (You can think of
cargo run as an analog to
Now we are ready to prepare Visual Studio Code for Rust development!
Step 3: Install the
rust-analyzer Visual Studio Code extension
The most important Visual Studio Code extension to install is
rust-analyzer. You can think of
rust-analyzer, you are basically just using Visual Studio Code as a text editor without all of the additional type checking, lints, and code completion that you expected.
Once installed, you can open up
src/main.rs (or any other Rust file) and you’ll get the Visual Studio Code experience that you expect. (If you get an error message saying that
rust-analyzer couldn’t find a Rust workspace, that just means the
Cargo.toml file is missing or invalid.)
Important: Make sure you
cargo run your project at least once before opening a
.rs file in Visual Studio code. The
rust-analyzer extension requires the build information found in the
target directory in order to do things like code completion and symbol lookup. If it seems like you aren’t getting any of that info in Visual Studio Code, stop and run
cargo run. If that still doesn’t work, make sure the file you are working on is referenced in
lib.rs. Files that aren’t referenced by your primary file in some way aren’t compiled and so
rust-analyzer doesn’t know about them.
Step 4: Install the CodeLLDB Visual Studio Code extension
Out of the box, you won’t be able to debug Rust code in Visual Studio Code. You’ll need to install either the CodeLLDB extension or the Microsoft C/C++ Tools extension. Because the Rust compiler is build on LLVM, you’ll need one of these two extensions to generate the debug information you’ll need to debug Rust programs in Visual Studio Code. Most folks seem to prefer CodeLLDB, like because it’s not Microsoft-related, but either will work.
Bonus Step: Install the
crates Visual Studio Code extension
While this extension isn’t strictly necessary, the
crates extension helps ensure that you are using the most up-to-date versions of your dependencies. It does this by placing a small green checkmark to the right of each dependency in your
It took me several days to get Visual Studio Code set up for Rust development by finding all of the various tools and extensions that needed to be installed to work properly. Once set up, though, Visual Studio Code is just as suitable a development environment as any other editor or IDE could be for Rust. The