Rust Analyzer Vscode



I have used many editors in the last 5 years.

  1. Rust Analyzer Vscode Not Working
  2. Rust Analyzer Vs Rls
  3. Vscode Rust-analyzer Rustfmt
  4. Rust Analyzer Vscode
  5. Rust Analyzer Server

Regardless of which OS you use I recommend getting Rust Analyzer (RA). It has excellent IDE support for Rust and is being actively developed. If you’ve heard of RLS then Rust Analyzer is a replacement for that (often dubbed RLS 2.0). It is now available on the VSCode Marketplace. # rust # vscode. Thiago Massari Guedes May 13, 2020 ・2 min read. Rust analyzer; After installing, open a rust file in the editor and you will be asked. Install the rust-analyzer extension; Configuration. This extension provides configurations through VSCode's configuration settings. All the configurations are under rust-analyzer. See for more information on VSCode.

Sublime Text, Vim, then CLion, then VSCode, back to Vim, briefly Onivim and now Neovim.

I find it important to experiment with different editors and IDEs in order tohave an idea of what powers they hold and how they could be included in yourdevelopment toolbox.

Over the last couple months, I have been looking at ways to “sharpen” my developmenttoolset and have been playing around with multiple vim configurations, one in whichI’d like to share today.

Neovim is a fork of vim, which is focused on extensibilityand usability. An example of this is the ability to use Lua instead of VimL forplugins providing greater flexibility for extending the editor.

In the 0.5 release of Neovim (currently nightly), the developers have introducedan Language Server Protocol(LSP) client framework (:help lsp)

This means, Neovim can act as a client to LSP servers (like rust-analyzer) andassist in building enhanced LSP tools.

LSP facilitates programming language specific features such as go-to-definition,completion, refactoring, formatting, etc. The goal of LSP is to separatelanguage support and the editor.

Why use LSP? Well, for one, it allows the developers of an editor to focus on theeditor and not of specific language support. This is a win-win for language providersand those who release tooling. This is turning a X*Y problem into X+Y.(Where X is the number of editors and Y is the number of languages). There are LSPservers available for almost every language out there.

So how do we configure Neovim LSP with rust-analyzer? Simple!

Check out this repository for the complete configuration and more

Let’s start with the prerequisites:

  • Neovim >= 0.5, see Installing Neovim
    • Currently, 0.5 can be found as anightly download,in the unstable PPAor other nightly sources. I am currently living on the bleeding edge: buildingand installing neovim from the master git branch.
  • Install rust-analyzerNote: The binary must be in your PATH

Diving in, let’s install some plugins.

The plugin manager used here is vim-plug,but any plugin manager can be used.

To install the above run the :PlugInstall command in neovim, or start it with nvim +PlugInstall.

Enable syntax highlighting and file type identification, plugin and indenting

Let’s setup the rust-analyzer LSP and add completion and enable diagnostics

Now when nvim is restarted, you should be able to autocomplete and view warningsand errors inside the editor! You’ll notice, however, that the completion experienceis not like what you might be use to in VSCode or other editors.Mostly surrounding the lack of <Tab> completion to navigate the menu. Vim uses <C-N>!

<Tab> completion can be accomplished with the following

(Found in :help completion)

What about code navigation? (:help lsp)

Code actions are also very useful.

Let’s improve the diagnostics experience.

You may notice, there’s a slight vertical jitter when a new diagnostic comes in.

To avoid this, you can set signcolumn

And to cap it off, let’s enable inlay hints!

To conclude, this introduces a basic and flexible setup for Rust development.Here’s the best part though, it’s simple to configuremore languages servers!This setup allows you, the developer, to add more lsp'(just like we did with rust-analyzer) to have a full featured cross-language experience.

Thanks for reading!

Questions? Found an error? Create an issue on Github!

Edits:

  • 2020-09-23: Added note about signcolumn
  • 2020-10-05: Added note about code actions and gif
  • 2020-12-17: Updated diagnostics and lsp config to reflect latest neovim developments
  • 2020-12-23: Updated tab completion config to reflect latestcompletion.nvim developments
  • 2021-02-02: Added enabled to inlay_hints function call to support more hints

Visual Studio Code is my Rust editor of choice. Unfortunately it can't quite debug Rust out of the box.

Configuring the debugger isn't hard. But there are a few steps. I've gone through them several times now. I'm writing this guide to save future me from having to remember them.

Hopefully this guide is useful to a few other folks as well.

Install Rust and VS Code

Rust Analyzer Vscode Not Working

This should go without saying.

Install Rust
Install Visual Studio Code

Install VS Code Extensions

You'll need to install an extension. Which one depends on your platform.

C/C++ (Windows)
CodeLLDB (OS X / Linux)

It probably makes sense to go ahead and install the Rust extension as well.

Configure VS Code

Now that your tools are installed you need to configure your VS Code launch properties.

Click Debug -> Add Configuration
If you're on Windows then select C++ (Windows)
If you're on Mac or Linux then select LLDB: Custom Launch

Rust Analyzer Vs Rls

This should create and open launch.json. You'll have to manually change the executable name under 'program'.

Text above can be copy-pasta'd.

Next, you should verify breakpoints are enabled. Some readers have reporting needing to this do. Some machines have it enabled by default. 🤷‍♂️

File -> Preferences -> Settings

That's it!

Add a breakpoint. Press F5 to launch. Voila!

Limitations

Debugging Rust works pretty well. It's not perfect. But it's pretty good!

Basic types work fine. Assuming they aren't optimized away by the compiler, of course.

I've found the Rust compiler to be a little more aggressive than C++ when it comes to optimizing away 'unused' variables. Sometimes I store intermediate values in variables just for the debugger. Their absence can be mildly annoying.

Vectors work just fine. Thankfully. I wish the unexpanded 'preview' was more informative.

Vscode Rust-analyzer Rustfmt

Unfortunately other containers don't work at all. HashMap is indecipherable crap. :(

Visual Studio 2017 has natvis for C++. It's not great. I have a lot of complaints. It's way better than Rust's nothing.

Mixed Debugging

Rust Analyzer Vscode

While working on this post I learned something new. I'm somewhat blown away and want to share it.

Rust analyzer vscode

Rust Analyzer Server

I was experimenting with the microprofile library. It wasn't behaving quite like I expected so I stepped into the debugger. Much to my surprise I was able to seamlessly step into the crate's Rust code. But what really shocked me is I could also step right into it's underlying C++ code!