Even better ‘How to Debug Rust with Visual Studio Code’

I decided to start my adventure with Rust programming language and natural choice for the editor was Visual Studio Code. VS Code is the main general-purpose editor I use for almost every language. JavaScript, Typescript, Python, even Assembly.

There is no out of the box solution for Visual Studio Code to debug Rust. So the first action I did after creating a hello world project was googling the phrase ‘how to debug rust with visual studio code’. And I found a great guide: https://www.forrestthewoods.com/blog/how-to-debug-rust-with-visual-studio-code/

Problem

The guide linked above by Forrest Smith is fantastic and in detail explains how to configure Debug configuration. However, I have two problems with that:

  • the need for separating the same configuration (launch.json) for every project
  • in case project name will change you need to change the binary path in debug config

In general, you just need to create the same configuration for every rust project you establish and I’ve had a problem with that.

Solution

Instead of creating seperate launch.json for every file we will add launch configuration to user settings.json .

To solve the problem with "program" parameter in configuration, instead of giving static filename it will be replaced with Visual Studio Code variable ${workspaceFolderBasename} . As long as we are using cargo new command to create a new project, the project directory will be the same as the executable name (without extension).

Prerequisites

Basing on your platform, install one of the extensions below:

C/C++ (Windows)
CodeLLDB (OS X / Linux)

Visual Studio Code settings

Open the settings.json file from the Command Palette (Ctrl+Shift+P) with the Preferences: Open Settings (JSON) command.

Add one of below configuration depending on your platform to settings.json file:

Windows

"debug.allowBreakpointsEverywhere": true,
launch":  {
    "version": "0.2.0",   
    "configurations": [        
    { 
        "name": "(Windows) Rust Launch",            
        "type": "cppvsdbg",            
        "request": "launch",            
        "program": "${workspaceRoot}/target/debug/${workspaceFolderBasename}.exe",            
        "args": [],            
        "stopAtEntry": false,            
        "cwd": "${workspaceRoot}",            
        "environment": [],            
        "externalConsole": true        
    }
]}

Linux / OS X

"debug.allowBreakpointsEverywhere": true,
launch":  {
    "version": "0.2.0",   
    "configurations": [        
    {
        "name": "(Linux/OSX) Rust Launch",
        "type": "lldb",
        "request": "launch",
        "program": "${workspaceRoot}/target/debug/${workspaceFolderBasename}",
        "args": [],
        "cwd": "${workspaceRoot}",
    }
]}

Please ensure that your settings.json will remain valid .json file, otherwise your settings won’t take effect.

You are ready to go. Save settings.json file, set a breakpoint and press F5 to start Debug. Enjoy!