Getting Started with Rust on Windows and Visual Studio Code

Rust is an exciting programming language and I highly recommend giving it a shot. Getting started is a cinch. If you’re interested in programming Rust on Windows, follow the steps below and you’ll be up and writing code in no time.

Watch the Video On YouTube

If you would rather follow in video format, you can catch a video of this post on my YouTube channel.

Prerequisites for Using Rust in VS Code

Interestingly enough, the Rust compiler requires the Microsoft C++ Build Tools to do its job. Before proceeding, you’ll need to ensure the Visual Studio Build Tools are installed on your machine. Visual Studio comes prepackaged with these build tools. However, VS Code does not. You can find the Visual Studio Build Tools 2019 version here. It is free to download and install.

Clicking the link above will take you to a page entitled, “Thank you for downloading Visual Studio.” Don’t be fooled by this title. the download of the build tools will begin shortly after clicking the link.

Once you’ve downloaded the build tools, execute the setup. Select the option to install the C++ Build Tools and click the Install button. Upon completion, close the window entitled “Visual Studio Installer.”

Install Rust

Next, head on over to the Installation page and click the gargantuan button labeled “Rustup-Init.exe.” There are a few alternate ways to install Rust, but I don’t recommend them. Even though you may be tempted, I strongly suggest not installing Rust through Chocolatey, as it doesn’t appear to install all of the required tools in the tool chain (mainly rustup). With rustup missing, you won’t be able to get the Rust Language Server (RLS) up and running in Visual Studio Code.

When you click on the button, your browser will download the rustup-init.exe executable, which is essentially a command-line installer. Proceed along with the defaults until the installation is complete. Installing Rust will alter your machine’s PATH environment variable. So once the installation is complete, you’ll need to launch a new command prompt for changes to take affect.

Verify Rust is Installed

Open a new PowerShell window or command prompt and execute the following:

rustup --version

If you see a version number, the tool chain should be successfully installed. You can then verify that the latest version of Rust and all of the other tools in the tool chain are installed with the following command line:

rustup update

Configure Visual Studio Code

Launch VS Code. Note that if VS Code was running before the Rust installation completed, you’ll need to restart it for the environmental changes to take affect.

There are a variety of VS Code extensions that support Rust. I found the “Rust Extension Pack” to be a great pick on the marketplace. Not only does it bundle the Rust RLS official extension, but it includes the most popular cargo and TOML plugins available. To install this extension pack:

  1. Navigate to the Extensions panel (or type Ctrl-Shift-X).
  2. Enter “Rust Extension Pack” in the search panel.
  3. Click the Install button.

The only deficiency in the Rust Extension Pack is the lack of test integration. Unfortunately, there currently isn’t a lot to choose from. However, there is one that appears to do a decent job – “Rust Test Explorer“. Follow the steps above to install this extension and you’ll be able to navigate and execute unit tests from the Test Explorer panel.

Create a Simple Hello World Project

In VS Code, open a new, empty folder. We’ll flesh out a simple Rust project from here. In the Explorer panel, create a new file named “Cargo.toml” with the following information:

[package]
name = "hello-world"
version = "0.1.0"

Create a new directory named ‘src.’ Within that directory, create a file named main.rs with the following content:

fn main() {
    println!("Hello World!");
}

Create a Build Task

VS Code will execute a default task every time you choose to build your code. We will configure Code to execute the “cargo build” command line every time you kick off this build process.

Open the command palette (Ctrl-Shift-P) and type in “build” and select “Tasks: Configure Default Build Task”

Select “Rust: cargo build”

This should create a tasks.json file that has a single, default build task.

Cargo Build Task in Tasks.json

Cargo Build Task in Tasks.json

Now, every time that you press Control-Shift-B or run a build from the command palette, VS Code will execute cargo build.

Configuring Unit Tests

Just like with the Build task, Code also executes a default task when you choose to run unit tests. To get you up and running, we’ll create a simple test that always fails. Open the main.rs file and add a simple test below the main function:

#[test]
fn should_fail() {
    unimplemented!();
}

Save the file and open the command palette again. Type in “task” and select “Tasks: Configure Default Test Task”

Select “Rust: cargo test”

This should add a cargo test task to the tasks.json file. Now, every time that you run the Test Task, VS Code will execute the “cargo test” command and provide feedback in the Terminal panel. If you would prefer to execute your unit tests from a UI, you can also execute tests directly from the Test Explorer.

If your tests aren’t showing up in Test Explorer, press the circular refresh button to refresh the test list.

Rust Unit Tests in Test Explorer

Rust Unit Tests in Test Explorer

Configure Debugging Configuration

The thought of typing “cargo run” every time I want to run my app is not very enticing. I also want to be able to debug my app. I thought debugging Rust in Code was going to a be a pain until I found a blog by Bryce Van Dyk, entitled “Debug Rust on Windows with Visual Studio Code and the MSVC Debugger.” His post is awesome; read through it if you get a chance.

If you don’t want to read the whole article and still want to take advantage of debugging Rust in Code, perform the following steps:

  1. Install the C/C++ extension.
  2. Install the Native Debug extension.
  3. In the Debug panel, click Show all Automatic debug configurations.
  4. Click Add Configuration…
  5. Select the “C++ (Windows)” environment. This will create a new launch.json file.
  6. Change the program value to be “${workspaceFolder}/target/debug/hello-world.exe” and save the file.

Your launch.json should look similar to mine:

Rust Debug Configuration in Launch.json

Rust Debug Configuration in Launch.json

Next, you’ll need to alter your settings to allow breakpoints in Rust files.

  1. Click File | Preferences | Settings. The settings Tab will open.
  2. In the Search Settings textbox, enter the text “breakpoint”.
  3. Click the checkbox to allow setting breakpoints in any file.

Alternatively, you can open your settings.json file and add the following:

{
    "debug.allowBreakpointsEverywhere": true
}

Now, when you hit F5, VS Code will start debugging a new instance of your program. You can add breakpoints where needed.

Rust Debug Breakpoint Hit

Rust Debug Breakpoint Hit

4 Comments

  1. Scott Jones November 12, 2019
  2. Srinivas Sampath December 26, 2019
  3. Thomas Cox February 23, 2021
    • Bryan Burman February 23, 2021

Reply Cancel Reply