Lewis Cianci I'm a passionate mobile-first developer, and I've been making apps with Flutter since it first released. I also use ASP.NET 5 for web. Given the chance, I'll talk to you for far too long about why I love Flutter so much.

How to debug Rust with VS Code

4 min read 1399

How To Debug Rust With VS Code

Rust continues to uphold its status as the most-loved programming language for its ease of use, safety, and high performance. With this popularity, many new developers are learning Rust for the first time.

One important task when setting up for Rust development locally is to choose a development environment, then set up that environment to do what you want it to. Many developers choose to debug their code in Visual Studio Code, due to the fact that it is free and feels snappy to use on most hardware.

Let’s see how we can get set up with Rust on Visual Studio Code for debugging! 🧑‍💻🚀

Jump ahead:

Benefits of using VS Code to debug Rust

VS Code benefits from being available across a variety of platforms, so whether you’re coding on Linux, Windows, or macOS, you’ll always be able to develop and debug Rust.

The development experience is also first-class, as the Rust plugin for VS Code uses rust-analyzer within your Rust project as well. By itself, this means that you get excellent code hints and recommendations within VS Code itself.

It’s also the right price — free! — and performs well. If you are comparing using VS Code to another IDE like CLion, VS Code stands up surprisingly well. And, if you’re already developing software, there’s a good chance you already have it installed on your computer right now.

Check out this in-depth comparison between VS Code and other IDEs to learn more.

Actions available for debugging Rust with VS Code

When you use the rust-analyzer extension for VS Code, it provides an array of code linting features, which help you write higher-quality code. For example, if we create an enum and don’t use it, VS Code will display a warning indicating that the declaration of this enum is redundant:

Vs Code Terminal With Dark Background Displaying Popup Warning That Enum Declaration Is Redundant

If we then use the getSimpleEnum function within our main function, rust-analyzer will prompt if we haven’t used all the results from our match call:

Vs Code Terminal With Dark Background Displaying Popup Prompt Listing Missing Match Arms

Better still, the suggestions from rust-analyzer will automatically generate our match arms for us:

Vs Code Terminal With Dark Background Showing Four Automatically Generated Enum Match Arms

rust-analyzer has many more features available for us in VS Code. Check out the User Manual to see them all.

With VS Code installed on our computer, let’s get started with setting up Rust 🚀

Setting up Rust on your computer

On macOS or Linux, installing Rust is as simple as typing the following command in your terminal:

$ curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh

On macOS, you’ll also need a C compiler, which you can get by typing xcode-select --install in the terminal.

If you’re installing Rust on Windows, the setup is a little different. You’ll need to first retrieve Rust from the website, then install the build tools for Windows. These are part of Visual Studio 2022, so make sure you install the “Desktop Development with C++” workload as well as the Windows 10 or 11 SDK.

After you’ve done all that, you should have the Rust CLI installed on your computer. You can check by opening up a command window or terminal and typing rustc --version. The output should show the version of Rust that you currently have installed, like so:

Command Window With Black Background Displaying Command And Output For Showing Currently Installed Version Of Rust

Creating our Rust project

While you are still in your command window, navigate to the directory where you would like to create your project. Then run cargo new rustdebugging. rustdebugging is the name of our project for this tutorial, but you can call your project something else if you would like to.

After a second or so, you should see this message appear in your window:

Command Window With Black Background And White Text With One Green Text Word "Created" Displaying Command And Confirmation Message For Creating Rust Project

Now change directories into our new project by typing cd rustdebugging. Then, typing code . will open Visual Studio Code in that directory.

Setting up VS Code for debugging Rust

When we first open our project, we need to indicate that we trust the project when we are prompted, otherwise we won’t be able to do anything:

Vs Code Window With Popup Message Prompting User To Confirm Trust In Authors Of Project

Next, we need to install the rust-analyzer. This is an integral part of compiling and debugging Rust code from within VS Code. Navigate to the extensions marketplace within VS Code, then find and install the rust-analyzer package:

Vs Code Extensions Marketplace With Rust Analyzer Extension Pulled Up

Then we can open a terminal window from within VS Code, and execute cargo build:

Vs Code Terminal Window With File Explorer Panel On Left And Main Rs File Opened On Right Displaying Finished Result Of Executing Cargo Build Command

After doing that, we should have a new path within our project of target/debug/rustdebugging.exe.

Setting breakpoints in VS Code

Open up your main.rs file, and observe the existing “Hello, world!” statement. If we click to the left of the line numbers, we can set a breakpoint for that particular line:

Vs Code Terminal With Red Arrow Pointing To Left Of Line Two Where User Should Click To Set Breakpoint

Once we have set a breakpoint, there will be a little red circle on that line:

Closeup View Of Line Two In Vs Code Terminal With Red Circle Showing That Breakpoint Has Been Set

If we hit F5 on the keyboard, a new console window should appear, and our code should stop on that line. We should also see the debug windows appear in VS Code, such as the Variable view and Call Stack. Our “Hello, world!” line is highlighted in yellow, indicating that is the line that execution is halted on:

Vs Code Terminal With Line Two Highlighted In Yellow To Show That Execution Has Been Halted At The Breakpoint

Now, let’s make our code more complex by adding together two numbers. Replace the contents of main.rs with the following:

fn main() {
    let applesThatJaneHas = 3;
    let applesThatBobHas = 2;
    println!("The total amount of apples that everyone has is {:?}", applesThatBobHas + applesThatJaneHas);
}

Now, if we reapply our breakpoint on the println invocation and press F5, our code still stops on that line. But we can see that we have our local variable view on the left in VS Code:

Vs Code Terminal With Two Double Sided Red Arrows Pointing Between Function Lines And Their Respective Local Variable Views. Highlighted Line With Breakpoint Now Appears At Line Four

Updating our apple counts to a vec are also visible in the debugger:

Vs Code Terminal With Double Sided Red Arrow Pointing Between Vec And Corresponding Local Variable View

Even other more complicated types like HashMap are explained well in the local variable view. Replace the code in main.rs with the following:

use std::collections::HashMap;
fn main() {
    let mut apple_map = HashMap::from([
        ("Red Delicious", 2),
        ("Kawana Apple", 5),
    ]);
    println!("The total amount of apples that everyone has is {:?}", apple_map);
}

Vs Code Terminal With Three Double Sided Red Arrows Pointing Between Hashmap Lines And Corresponding Local Variable Views

With your breakpoints being hit, and the variables appearing in the view, you’re now all set to debug your Rust programs in VS Code!

Limitations of using VS Code to debug Rust

While VS Code is an excellent tool for developing Rust projects, developers should bear in mind that it isn’t technically a full IDE.



While this can be a good thing — for example, in terms of VS Code feeling responsive to use — it also means that some advanced functionality may not be present in VS Code, or may have to be added through extensions.

Another possible issue with VS Code is that, unlike many other developer tools, it’s not possible to tear off tabs from within the editor into new windows. An issue to do with this functionality has been open for quite some time, and shows no sign of changing soon.

This doesn’t have to be a big problem, but for many developers who are used to this functionality, it’s something that’s not part of VS Code today.

Conclusion

Now you are all set up for developing Rust projects in Visual Studio Code. You can take this opportunity to install more extensions into VS Code to help your development workflow. Take a look at a few suggestions below.

Rust uses a cargo.toml file to track dependencies and other project metadata. Using the Better TOML extension gives you syntax highlighting and validation.

Use Crates to check whether you are using the most up-to-date crates within your project. This extension helps with updating dependencies, whether one at a time or all at once.

For more advanced debugging for Rust projects within VS Code, check out the CodeLLDB extension. Amongst other features, this extension lets you set conditional breakpoints.

If you’re getting started with Rust, now would be an excellent time to refer to the official Rust Programming Language tutorials, such as how to program a guessing game. These tutorials provide an excellent introduction to the Rust language as a whole and will help you on your journey to becoming a Rust developer.

Once you have your workspace set up, you’ll be able to work on Rust projects with ease, regardless of your operating system. Happy coding! 🦀

LogRocket: Full visibility into web frontends for Rust apps

Debugging Rust applications can be difficult, especially when users experience issues that are difficult to reproduce. If you’re interested in monitoring and tracking performance of your Rust apps, automatically surfacing errors, and tracking slow network requests and load time, try LogRocket.

LogRocket is like a DVR for web and mobile apps, recording literally everything that happens on your Rust app. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred. LogRocket also monitors your app’s performance, reporting metrics like client CPU load, client memory usage, and more.

Modernize how you debug your Rust apps — .

Lewis Cianci I'm a passionate mobile-first developer, and I've been making apps with Flutter since it first released. I also use ASP.NET 5 for web. Given the chance, I'll talk to you for far too long about why I love Flutter so much.

One Reply to “How to debug Rust with VS Code”

  1. At first I thought the rust-analyzer by default would allow for simple debugging with breakpoints, but at least for me I needed CodeLLDB installed first to be even able to put a breakpoint. Great quick read though.

Leave a Reply