Debugging Rust Projects in VS Code and Terminal

Overview

Compared to other tools, Visual Studio Code is a great code editor providing seamless integration with Rust projects in terms of LSP (Language Server Protocol) and DAP (Debugger Adapter Protocol). Configure LSP for Rust is easy for all the Rust tools but setting DAP for debugging may not be so straight forward. If you prefer a light-weight code editor with seamless DAP integration then VS Code is a good choice.

Getting Started

Install Rust by following the instructions here.

Install Visual Studio Code.

From VS Code, press Ctrl-P (Command P for macOS), and enter to setup LSP support for Rust. You may be prompted to install other components by the extension.

Below are the different ways that you can use Visual Studio Code to debug your Rust projects.

LLDB Native Debugger

The LLDB Debugger is the debugger component of the LLVM project.

In Visual Studio Code, press Ctrl-P (Command P for macOS), enter to install CodeLLDB.

When it runs for the first time, the extension detects the Cargo project settings and creates a launch.json. After that you can set breakpoints and press F5 to start debug your application.

Cargo Debug

We are going to use cargo-debug instead of

Install cargo-debug

# cargo install cargo-debug

Once installed you can use lldb to debug the code in terminal.

Debug using the LLDb debugger

# cargo debug --debugger=lldb

Once you are in lldb debugger, you can set breakpoint

E.g. sets a breakpoint at line 1.

Enterto start debugging.

Create tasks.json under .vscode folder with the following content.

Press in macOS) to run the task to launch the lldb terminal.

For debugging Rust code in Neovim, check out the following article.

Do also check out the following articles.

Programmer and occasional blogger.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store