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.

Install Rust

First thing’s first. 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.

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.

Configure Debugging Configuration

The though 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, add a new configuration.
  4. Specify the “C++ (Windows)” environment. This will create a new launch.json file.
  5. This will create a new launch.json file. Change the program value to be “${workspaceFolder}/target/debug/hello-world.exe” and save the file.

Your launch.json should look similar to mine:

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

Leave a Reply