Debugging Rust with VS Code
Tags: rust, vscode, debug, beginners
Published: 2021-04-09 13:54:08

_(There's a translation to portuguese [here](https://dev.to/rogertorres/debugando-rust-no-vs-code-5ii))_

At the very beginning of my career (2008), I became an Oracle PL/SQL developer. I still don't know exactly why, but we didn't use any debugger back then. To fix the bugs, we would add messages everywhere and try to isolate the problem. The smarter kids would take a "binary search" approach, creating a message every 100 lines, finding out between which lines the problem was (say, between 300 and 400), and then refining the search, placing messages every 10 lines (310, 320 and so on). 

When I moved from working with Oracle to work with SAP and its ABAP language (which has a fantastic debugger), I discovered a new world. Then, when the time came for me to change technology again (to Rust) I was suddenly dreaded this question: "will I have a debugger there?"

This is the answer I found.

-----

## What you came for: debugging Rust in VSCode

*First*, you need an extension.
- On **Unix** (Mac, Linux, ...)? Get [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb)
- On **Windows?** Get [C/C++](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)

_Why_ do you need those?
- **Short answer**: Rust compiler uses LLVM to interact with the machine, and these extensions allow us to interact with the code that runs at this level (since, you know, Rust is compiled).
- **Long answer**: Above my paygrade, I'm just the support guy installing things on your computer; instead of asking me, check [this](https://www.infoworld.com/article/3247799/what-is-llvm-the-power-behind-swift-rust-clang-and-more.html).

Now, in your VSCode, follow these steps: `Run` > `Start Debugging` > `Ok` > `Yes`
![First step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftl8okpayl8084eubg8d.png)

![Second step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a4qb4ntz6jgok1h7eyjk.png)

![Third step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6j52awfjvl5fgsj2laui.png)

The expected result is the creation of the file `/.vscode/launch.json` that looks something like this:

```json
// File copied from MacOS X
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug unit tests in library 'yourprogram'",
            "cargo": {
                "args": [
                    "test",
                    "--no-run",
                    "--lib",
                    "--package=yourprogram"
                ],
                "filter": {
                    "name": "yourprogram",
                    "kind": "lib"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}
```
Or this, if you're on Windows:
```json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceRoot}/target/debug/yourprogram.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "externalTerminal"
        }
    ]
}
```

If for some reason I am unaware of, the files were not automatically created, you can copy and paste it (create the _.vscode_ folder at the same level as the _src_ folder).

Besides the example files being from a different OS, in the first one, we have a library crate, while the second one is a binary. If you have both crates in the same project, you will have two or three entries under `configurations` (depending on whether you have tests or not).

Now, theoretically, you could add a breakpoint to your code (clicking on the left column, creating this red dot).![Fourth Step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztrklc0dil1rlyr60jg1.png)If you can't, it is because this option is deactivated. To solve it, open your **Settings** and go to **Debug** (under *Features*—you can type "breakpoint" in the search bar to find it quickly) and check the __"Allow Breakpoints Everywhere"__ option.![Allow Breakpoints Everywhere](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0knvx5zuugso8ag9xakx.png)

With your breakpoints set up, press `F5` or go to `Run` > `Start Debugging` (which should work now that you have the file). You should end up with something like this:![Fifth Step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4t3v8ipylq8jk98i68s.png)

Basically, on the left, you have the values loaded on the memory. Things are pretty straightforward while you're handling types like these integers, but can become quite messy when you load things like HashMaps; still, it is far better than nothing.

_Didn't work out for you?_ Comment here, for there is a considerable chance that someone else will have a similar problem.

## Debug actions
![Actions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/61xoqkjfe8b9lcwasws9.png)To wrap it up, these are the actions you may perform while debugging are (from left to right):
- `F5` _**Continue**_: runs the program until it reaches another breakpoint;
- `F10` _**Step Over**_: Executes the step without going in; eg. if it is a function, it will not go into it;
- `F11` _**Step Into**_: Executes the step by going into;
- `Shift`+`F11` _**Step out**_: Eg. if you're in a function, it goes back to the caller.
- `Shift`+`Cmd`/`Ctrl`+`F5` _**Restart**_: Execute from the beginning.
- `Shift`+`F5` _**Stop**_: Halt the execution.

That's it. Now you're smarter than the smart kids from my first job :)

_Cover photograph by [Danilo Batista](https://unsplash.com/photos/ljDl1LoPxRY)_.