Abiodun Solomon I’m a software developer that is curious about modern technologies. I love contributing to the growth of knowledge for the betterment of humanity.

Intro to rust-analyzer

3 min read 981

Intro to Rust Analyzer

rust-analyzer is a frontend compiler for Rust programming. In other words, it’s an integrated development environment (IDE) server that provides semantic analysis, smart code completion, and more for your Rust code.

Prerequisites

To follow along with this tutorial, you’ll need:

Objectives

The goals of this tutorial are to break down the benefits of using rust-analyzer over other compilers, demonstrate how to set it up with VS Code, and walk through using some of the most useful features.

Key terms

You’ll need to understand the following basic terms.

  • Rust Language Server (RLS) — An IDE server that is similar to rust-analyzer but has a different architecture. It runs its compiler on a whole project and builds up a large JSON file that includes the information derived from the process. It checks and monitors every function code structure after every keystrokes and restructures it when checked. It also uses Racer for its code completion because its compiler gets slow during this process
  • Language Server Protocol (LSP) — A protocol designed to develop rich code features, such as smart code completions, goto code definition, linting, search and much more for IDEs and code editors using a language server. Most languages emulate this protocol because it helps to define a standard for various IDEs and editors

Why rust-analyzer?

rust-analyzer is developed to monitor user keystrokes, which helps it maintain a persistent compilation process as it analyzes the code on changes. In other words, it analyzes only the code in the file that’s currently active or opened in the IDE or editor.

Installing rust-analyzer

rust-analyzer is available for most IDEs with LSP support. To name a few, Vim, Emacs and VS Code have plugins for rust-analyzer.

You can install the plugin directly from the Visual Studio Marketplace.

Rust Analyzer in VS Code Marketplace

You’ll be prompted to install the rust-analyzer server to make the plugin work.

Install Rust Analyzer Server

To get regular updates, you’ll be prompted to install Nightly on VS Code.

You could also install the rust-analyzer server from binary by following the appropriate procedure below.

  • Mac: rust-analyzer-mac
  • Windows: rust-analyzer-windows.exe
  • Linux: rust-analyzer-linux

The command below is meant for Mac and Linux users. Windows users can easily run the executable file directly.

curl -L https://github.com/rust-analyzer/rust-analyzer/releases/latest/download/rust-analyzer-mac -o ~/.local/bin/rust-analyzer


chmod +x ~/.local/bin/rust-analyzer

Make sure the ~/.local/bin is added or listed in your environment variable.

Features of rust-analyzer

Now let’s walk through how to use some of rust-analyzer’s key features.



Definition

Definition refers to the process of locating a function/struct/impl/mod/pub source code context from the declaration or initialization using the following shortcut.

  • F12 / fn F12 on mac
  • Ctrl + click / Command + click

profile.rs

pub fn display_fullname(name: &str) {
  println!("Welcome back, {}");
}

main.rs

mod profile;

fn main() {
  profile::display_fullname("Abiodun Solomon");
}

Execute the shortcut above by moving the cursor to the function display_fullname() on main.rs and then hitting fn + F12.

Symbol search

This feature is implemented on existing code search in VS Code, which uses fuzzy search to interface all available symbols — such as structs, enums, functions, etc. — in the whole project.

To do this, hit ctrl/cmd + T , then start the search with the # sign followed by the symbol name.

Symbol Search

Symbol file analyses

Similar the symbol search, you have to start the search with @ sign or use ctrl/cmd + shift + o. This enables you to critically analyze the code snippets of that particular file and easily navigate on the search pane by switching between code snippets in the same file.

Symbol File Analysis

Syntax highlighting

rust-analyzer provides full-fledged syntax highlighting, which helps make your code more readable and understandable. This feature also helps you easily identify bugs in code when the code color changes unexpected.

Code execution

This feature makes it simple to run and debug your code, provided you have CodeLLDB or MS C++ installed. These icons are always at the top of the main function.

Code Execution

After clicking Run, you should see the terminal building and executing your code without the hassle of typing cargo build and cargo run.

Code Execution Terminal

Datatype detection

Datatype detection helps you identify the type of data that is assigned to a variable and displays it in gray text at the front of the variable name. It does the same for the function argument by appending the parameters to the front of the value.

Data Type Detection

The i32 in light gray is added by rust-analyzer to help you understand the type of data available in those variables.

fn display_name(name: &str) {
  println!("Welcome back, {}", name);
}

Data Type Detection

Error popup

This feature helps you spot errors in your code before execution. A red highlight displays underneath the function and defines the error when hovered over.

Error Group

Click on peek problem to see the full error message.

Peek Problem

Auto-create missing module

This useful feature enables you to create a missing module when not available in the project by clicking the Quick Fix on the popup dialog box. You could also use ctrl/cmd + . instead.

Autocreate Missing Module

Click on create module to create a new file called manager.rs, which can be found in your project directory.

Create Module

Implementation locator

This feature lets you index struct declarations throughout the whole project. It’s particularly useful when you have a large number of structs in your project.

Implementation Locator

Implementation Locator

The above example is peeking the struct implementation in a single file. The struct declaration and usage are both in the same file. You could also peek the implementation when the struct and the usage are both in different Rust files, depending on the structure of your project.

Conclusion

You should now have a clear understanding of the Language Server Protocol and the difference between rust-analyzer and Rust Language Server. With the knowledge you gained from this tutorial, you’re ready to take full advantage of the semantics analysis and smart code completion capabilities of rust-analyzer.

LogRocket: Full visibility into web frontends for Rust apps

Debugging Rust applications can be difficult, especially when users experience issues that are difficult to reproduce. If you’re interested in monitoring and tracking performance of your Rust apps, automatically surfacing errors, and tracking slow network requests and load time, try LogRocket.

LogRocket is like a DVR for web and mobile apps, recording literally everything that happens on your Rust app. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred. LogRocket also monitors your app’s performance, reporting metrics like client CPU load, client memory usage, and more.

Modernize how you debug your Rust apps — .

Abiodun Solomon I’m a software developer that is curious about modern technologies. I love contributing to the growth of knowledge for the betterment of humanity.

Leave a Reply