14

I am trying to debug a Rust program in VS Code, but I get an error: https://i.sstatic.net/QapPo.png

After clicking OK, VS Code opens "settings.json": https://i.sstatic.net/X17Ss.png

I have these extensions installed:

https://i.sstatic.net/HPpZ6.png

My program is a simple "hello world" app.

3 Answers 3

22

Unfortunately VS Code can't debug Rust out of the box :( But no need to worry, just few steps of configuration will do the work :)

Steps

  1. Install C/C++ extension if you are on windows and CodeLLDB if on OS X/Linux

  2. Click Debug -> Add Configuration, a launch.json file should open, you need to change the program name here manually

     {
         "version": "0.2.0",
         "configurations": [
             {
                 "name": "(Windows) Launch",
                 "type": "cppvsdbg",
                 "request": "launch",
                 "program": "${workspaceRoot}/target/debug/foo.exe",
                 "args": [],
                 "stopAtEntry": false,
                 "cwd": "${workspaceRoot}",
                 "environment": [],
                 "externalConsole": true
             },
             {
                 "name": "(OSX) Launch",
                 "type": "lldb",
                 "request": "launch",
                 "program": "${workspaceRoot}/target/debug/foo",
                 "args": [],
                 "cwd": "${workspaceRoot}",
             }
         ]
     }
    
  3. Make sure Allow setting breakpoints in any file is checkend under File -> Preferences -> Settings

For detailed steps and more you can refer the article I used to answer this

Credits- Forrest Smith

Sign up to request clarification or add additional context in comments.

3 Comments

This works! As of 2023-10 for Rust 1.75 on Windows 10 x64 VS Code 1.83. Two different MS/VSCode guides say CodeLLDB should work but I could never get the .json configured right and would get an "unknown error" at runtime, despite "cargo run" working fine. So I Uninstalled CodeLLDB and used only C/C++ v1.18 and using this config above worked--but why wouldn't it auto-generate a working .json for us like CodeLLDB does? And, now I still get an exit code 0xc0000135 and 'externalConsole' is deprecated, ugh onto another search...
As of April 2025 VS Code has no "Debug" menu. There's a menu called "Run" that has "Add Configuration", which opens settings.json, somewhere in my user profile (not in the project directory), with the following contents: { "launch": { "configurations": [] } }
When I download the example project from the linked article (at least it's not a dead link), I get the same error as OP gets. This answer is obsolete, please delete it. Also the answer would have been worthless without the linked article anyway, and links die.
16

From your screenshots, you're on Windows, so here's how to proceed. This assumes you already took care of the basics:

  1. VsCode has the recommended rust-analyzer extension installed.
  2. Your project folder was initialized with cargo init. (Your project's folder name must be the same as the name of your package in Cargo.toml.)
  3. You can cargo run from within your project directory and it works.

As indicated by various locations on the 'Net, you need to install another VsCode extension to make it so you can debug. Since you're on Windows, you want to use the MS C++ DevTools extension for VsCode, instead of the CodeLLDB one.

Next, you need a "launch" configuration setup. Select VsCode's Run >>> Add Configuration... menu item. Choose the C/C++: (Windows) launch option in the drop-down. You'll now have a launch.json file with a single configuration object.

You'll need to change the program property to "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe"; this depends on your package name being the same as the project folder's name. (I also changed the cwd property to "${workspaceFolder}", though I'm not sure it matters.) To be clearer, here's the configuration I have presently in my launch.json file (the preLaunchTask property is for later):

{
    "name": "(Windows) Launch",
    "type": "cppvsdbg",
    "request": "launch",
    "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
    "preLaunchTask": "rust: cargo build",
    "args": [],
    "stopAtEntry": false,
    "cwd": "${workspaceFolder}",
    "environment": [],
    "console": "externalTerminal"
}

At this point, as long as you've already built your project at least once, you can hit F5 and debug.

If you want F5 to also save your changes and rebuild your project before debugging, then you also have to add a build task and configure it to run before debugging starts.

To do that, add the build task by opening the Show All Commands box (either F1 or Ctrl+Shift+p) and choosing Tasks: Configure Task. Select rust: cargo build. It'll create a tasks.json file next to your launch.json; the defaults are all you need. My file looks like this:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cargo",
            "command": "build",
            "problemMatcher": [
                "$rustc"
            ],
            "group": "build",
            "label": "rust: cargo build"
        }
    ]
}

Then, to hook everything up, you just need to manually add the preLaunchTask property to your launch configuration with a value equal to the label in your task. E.g. "preLaunchTask": "rust: cargo build",, like what I have in my example launch.json up above.

At this point, whenever you press F5, VsCode will save your work, rebuild your project, then start debugging it.

1 Comment

When I press Run >>> Add Configuration..., it opens a big settings.json file with no dropdown options. In the bottom there is a thing that says: "launch": { "configurations": [ ] }, however when I paste the code into the list, it highlights a bunch of the entries as errors. However the error I get when running the code is now different - "Could not find the task `rust: cargo build`.. Adding the second code to launch.json doesnt fix that. Pressing "debug anyway" says configured debug type 'cppvsdbg' is not supported.
11

Visual Studio Code is a general editor, but it can be configured to debug rust code.

Step 1.

Assuming that Visual Code, rust and cargo are installed, the first step is to enable the required extensions:

  1. rust-analyzer
  2. CodeLLDB

These only need to be installed one.

Step 2

The second step is to create the rust code. I have a folder called Rust, in which I keep the rust code. Changing to that folder, I use cargo new hello_world to create a new rust project.

Step 3

The third step is to change the folder for the project. There are two plausible options, but only one of them will work.

I changed to my Rust folder, and then I can edit the source code by following hello_world - src.

enter image description here

To debug the code, it is necessary to create a launch.json file, using Run - Add configuration... However, the file isn't correct, with <your program> where the correct name should be. This is the wrong approach.

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "type": "lldb",
        "request": "launch",
        "name": "Debug",
        "program": "${workspaceFolder}/<your program>",
        "args": [],
        "cwd": "${workspaceFolder}"
    }
]

}

The documentation is a bit thin at this point. The correct approach is to pick a different folder, the top level of the project hello_world. The Cargo.toml file is available.

enter image description here

Now, when Run - Add configuration... is used, and the option of LLDB is selected -

enter image description here

the Cargo.toml file can be picked up -

enter image description here

and then the Cargo.toml file is used to correctly build the launch.json file -

  {
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
     {
        "type": "lldb",
        "request": "launch",
        "name": "Debug executable 'hello_world'",
        "cargo": {
            "args": [
                "build",
                "--bin=hello_world",
                "--package=hello_world"
            ],
            "filter": {
                "name": "hello_world",
                "kind": "bin"
            }
        },
        "args": [],
        "cwd": "${workspaceFolder}"
    },
    {
        "type": "lldb",
        "request": "launch",
        "name": "Debug unit tests in executable 'hello_world'",
        "cargo": {
            "args": [
                "test",
                "--no-run",
                "--bin=hello_world",
                "--package=hello_world"
            ],
            "filter": {
                "name": "hello_world",
                "kind": "bin"
            }
        },
        "args": [],
        "cwd": "${workspaceFolder}"
    }
]
} 

Now, both Run - Start debugging and Run - Run without Debugging both work properly.

3 Comments

This solution works for VS Code as in 2022.
Thank you for this. All the other options dont seem to work for VS Code in 2024
Alternative way is to specify Rust-analyzer Debug:Engine to be "vadimcn.vscode-lldb" in the Settings and then hit "debug" option to a Rust program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.