I’m not yet familiar with Rust, but now is as good a time as any other to start learning it. A couple of 2020 articles, “What is Rust and why is it so popular?” at the Stack Overflow Blog and “Why Discord is switching from Go to Rust” by Discord mention a few aspects that make Rust appealing to developers.
Usually, the first thing I do when starting to work with a new programming language is to figure out how to debug it. So in this article I’m going to show you how to get yourself set up to debug Rust using Visual Studio Code (VS Code), and then you can use that as a starting point and take your learning journey in any direction that is comfortable for you.
Installing Rust
The first thing you need is to install the Rust language itself and associated tools. Simply follow the Installation part of the Rust book to set up Rust for your operating system.
The cargo
tool which comes with the Rust installation is vital as it is used for various things from building and running Rust source code to managing packages (“crates”, in the Rust ecosystem).
Setting Up VS Code for Rust
If you don’t already have VS Code, head to its website and download it.
Then, you’ll need to install two extensions, based on the IDE Integration Using rust-analyzer
section of the Rust book, and the Debugging section of the Rust with Visual Studio Code documentation:
- rust-analyzer gives you most of the IDE integration you need between Rust and VS Code (e.g. Intellisense)
- Either the Microsoft C/C++ extension (if you’re on Windows) or CodeLLDB (for Linux/Mac) – this gives you the ability to actually debug Rust code
Creating a New Rust Project
Use cargo
to create a new Rust project as follows:
$ cargo new rust1
Created binary (application) `rust1` package
Then, open the newly created rust1 folder using either the terminal (as follows) or VS Code’s File -> Open Folder… menu option.
$ cd rust1
$ code .
Note that if you’re following the “Hello, World!” part of the Rust book and created a Rust program without cargo
, you won’t be able to debug it.
Debugging Rust with VS Code
You’ll find a “Hello world” program in src/main.rs under the folder you created (in this case rust1). Ensure you have that open in VS Code and the press F5. When prompted, select LLDB as the debugger to use.
At that point you get an error because you don’t have a launch configuration yet:
But that’s alright, because once you click “OK”, you’re offered the possibility to have a launch configuration generated for you, which you should gratefully accept:
When you click “Yes”, a file with launch configurations is generated for you:
See also my earlier article, “Working with VS Code Launch Configurations“, if you want to do more with these launch configurations in future.
Now you can switch back to main.rs and press F5 to debug the code. Click next to a line number to set a breakpoint if you want to stop anywhere. Try also hovering over parts of the code to get rich information about them.
Summary
If you prefer to debug Rust code in an IDE than do everything in the terminal, consider using VS Code. After installing both Rust and VS Code, all you need to do is install a couple of extensions, create a Rust project with cargo
, and generate launch configurations for it. Then you can debug your Rust code and benefit from all the IDE comfort (e.g. Intellisense) you’re used to in other languages.