configurar-vscode-rust

Configuring VS Code for Rust Programming Step by Step

  • 5 min

A development environment is the set of tools we use to write, run, and debug code comfortably.

We already have Rust installed deep in the system. Now we need to prepare the place where we will work without fighting with the editor every two minutes.

While you could program in Notepad (if you enjoy suffering), we are going to use Visual Studio Code (VS Code). It’s free, lightweight, and with the right extensions, it becomes a first-class IDE for Rust.

There are powerful alternatives like RustRover from JetBrains or Neovim if you’re a keyboard ninja. But VS Code is a very popular choice in the community.

In this article, we’ll configure VS Code to have intelligent autocompletion, real-time error detection, and most importantly, the ability to debug our code.

Installing VS Code

If you don’t have it yet, download and install it from its official website.

Once open, let’s go directly to the Extensions tab (the squares icon on the left sidebar or Ctrl+Shift+X).

The engine: rust-analyzer

Years ago, there was an extension simply called “Rust”, which eventually became obsolete. Today, the official extension is rust-analyzer.

Search for and install the rust-analyzer extension.

What does rust-analyzer provide?

It’s not just a syntax highlighter. This extension runs a language server in the background that deeply understands your code:

  1. Inlay Hints: Rust infers types, so sometimes you don’t write what type a variable is. rust-analyzer shows you, in gray, “ghost-like,” the type Rust has deduced. It’s very helpful for keeping track.
  2. Intelligent Autocompletion: It knows what methods your struct has or what variants an enum has.
  3. Live Error Detection: You don’t need to compile to see if you’ve made a mistake. It underlines the error instantly.

If you see a lot of “extra” gray text in your code that you didn’t write, those are the Inlay Hints. They might be overwhelming at first, but you won’t be able to live without them later.

Configuring the Debugger

Writing code is easy; fixing it is the hard part. By default, VS Code doesn’t know how to debug Rust. We need an additional extension that understands the binaries Rust generates.

We have two main options depending on your operating system:

CodeLLDB

Search for and install the extension called CodeLLDB (by author Vadim Chugunov). It works on Windows, Linux, and macOS, and usually requires little configuration.

C/C++ on Windows with MSVC

If you’re on Windows and have installed Microsoft’s C++ tools, you can also use Microsoft’s official C/C++ extension. Both work, but CodeLLDB usually requires less manual configuration.

Testing the Debugger

For the debugger to work, we need to create a launch configuration.

  1. Open your project (the test_installation folder we created earlier).
  2. Go to the “Run and Debug” tab (the bug icon with play).
  3. Click on “create a launch.json file”.
  4. If prompted for the environment, select LLDB.

The extension will create an initial configuration in .vscode/launch.json. Now you can set a breakpoint (red click to the left of the code line) and press F5 to stop execution and inspect variables.

Quality-of-Life Extensions

In addition to the basics, there are a couple of extensions that, in our experience, are essential for working with Rust:

Rust configuration files (Cargo.toml) use the TOML format. This extension provides syntax highlighting and validation to prevent errors when adding libraries.

This extension is very practical. When you open your Cargo.toml, it looks up library versions online and tells you if you’re out of date directly in the editor. It’s a huge help for managing dependencies.

Instead of having to hover over the red line to see the error, this extension displays the error at the end of the code line. It greatly speeds up fixing bugs.

To finish, let’s tweak a couple of settings in VS Code (Ctrl + ,) so that Rust behaves as it should.

Format on Save

Rust has an official style tool called rustfmt. Don’t waste time indenting your code.

  1. Open settings.
  2. Search for “Format On Save”.
  3. Enable it.
  4. Search for “Default Formatter” and make sure to choose rust-analyzer.

Now, whenever you save (Ctrl+S), your code will automatically be formatted according to the community standard.

Clippy instead of check

By default, rust-analyzer runs cargo check to find errors. But Rust has a tool called Clippy that not only finds errors but also gives you advice on improving your code (“this could be written better like this…”).

We can tell VS Code to use Clippy:

  1. Search for “Rust-analyzer: Check Command”.
  2. Change the value from check to clippy.