User Manual

At its core, rust-analyzer is a library for semantic analysis of Rust code as it changes over time. This manual focuses on a specific usage of the library — running it as part of a server that implements the Language Server Protocol (LSP). The LSP allows various code editors, like VS Code, Emacs or Vim, to implement semantic features like completion or goto definition by talking to an external language server process.

To improve this document, send a pull request:
https://github.com/rust-analyzer/…​/manual.adoc

The manual is written in AsciiDoc and includes some extra files which are generated from the source code. Run cargo test and cargo test -p xtask to create these and then asciidoctor manual.adoc to create an HTML copy.

If you have questions about using rust-analyzer, please ask them in the “IDEs and Editors” topic of Rust users forum.

Installation

In theory, one should be able to just install the rust-analyzer binary and have it automatically work with any editor. We are not there yet, so some editor specific setup is required.

Additionally, rust-analyzer needs the sources of the standard library. If the source code is not present, rust-analyzer will attempt to install it automatically.

To add the sources manually, run the following command:

1
$ rustup component add rust-src

VS Code

This is the best supported editor at the moment. The rust-analyzer plugin for VS Code is maintained in tree.

You can install the latest release of the plugin from the marketplace.

Note that the plugin may cause conflicts with the official Rust plugin. It is recommended to disable the Rust plugin when using the rust-analyzer extension.

By default, the plugin will prompt you to download the matching version of the server as well:

75067008 17502500 54ba 11ea 835a f92aac50e866

To disable this notification put the following to settings.json

1
{ "rust-analyzer.updates.askBeforeDownload": false }

The server binary is stored in:

  • Linux: ~/.config/Code/User/globalStorage/matklad.rust-analyzer

  • Linux (Remote, such as WSL): ~/.vscode-server/data/User/globalStorage/matklad.rust-analyzer

  • macOS: ~/Library/Application\ Support/Code/User/globalStorage/matklad.rust-analyzer

  • Windows: %APPDATA%\Code\User\globalStorage\matklad.rust-analyzer

Note that we only support two most recent versions of VS Code.

Updates

The extension will be updated automatically as new versions become available. It will ask your permission to download the matching language server version binary if needed.

Nightly

We ship nightly releases for VS Code. To help us out with testing the newest code and follow the bleeding edge of our master, please use the following config:

1
{ "rust-analyzer.updates.channel": "nightly" }

You will be prompted to install the nightly extension version. Just click Download now and from that moment you will get automatic updates every 24 hours.

If you don’t want to be asked for Download now every day when the new nightly version is released add the following to your settings.json:

1
{ "rust-analyzer.updates.askBeforeDownload": false }
Nightly extension should only be installed via the Download now action from VS Code.

Manual installation

Alternatively, procure both rust-analyzer.vsix and your platform’s matching rust-analyzer-{platform}, for example from the releases page.

Install the extension with the Extensions: Install from VSIX command within VS Code, or from the command line via:

1
$ code --install-extension /path/to/rust-analyzer.vsix

Copy the rust-analyzer-{platform} binary anywhere, then add the path to your settings.json, for example:

1
{ "rust-analyzer.server.path": "~/.local/bin/rust-analyzer-linux" }

Building From Source

Alternatively, both the server and the Code plugin can be installed from source:

1
2
$ git clone https://github.com/rust-analyzer/rust-analyzer.git && cd rust-analyzer
$ cargo xtask install

You’ll need Cargo, nodejs and npm for this.

Note that installing via xtask install does not work for VS Code Remote, instead you’ll need to install the .vsix manually.

If you’re not using Code, you can compile and install only the LSP server:

1
$ cargo xtask install --server

Troubleshooting

Here are some useful self-diagnostic commands:

  • Rust Analyzer: Show RA Version shows the version of rust-analyzer binary.

  • Rust Analyzer: Status prints some statistics about the server, and dependency information for the current file.

  • To enable server-side logging, run with env RA_LOG=info and see Output > Rust Analyzer Language Server in VS Code’s panel.

  • To log project loading (sysroot & cargo metadata), set RA_LOG=project_model=debug.

  • To log all LSP requests, add "rust-analyzer.trace.server": "verbose" to the settings and look for Rust Analyzer Language Server Trace in the panel.

  • To enable client-side logging, add "rust-analyzer.trace.extension": true to the settings and open Output > Rust Analyzer Client in the panel.

rust-analyzer Language Server Binary

Other editors generally require the rust-analyzer binary to be in $PATH. You can download the pre-built binary from the releases page. Typically, you then need to rename the binary for your platform, e.g. rust-analyzer-mac if you’re on Mac OS, to rust-analyzer and make it executable in addition to moving it into a directory in your $PATH.

On Linux to install the rust-analyzer binary into ~/.local/bin, this commands could be used

1
2
$ curl -L https://github.com/rust-analyzer/rust-analyzer/releases/latest/download/rust-analyzer-linux -o ~/.local/bin/rust-analyzer
$ chmod +x ~/.local/bin/rust-analyzer

Ensure ~/.local/bin is listed in the $PATH variable.

Alternatively, you can install it from source using the command below. You’ll need the latest stable version of the Rust toolchain.

1
2
$ git clone https://github.com/rust-analyzer/rust-analyzer.git && cd rust-analyzer
$ cargo xtask install --server

If your editor can’t find the binary even though the binary is on your $PATH, the likely explanation is that it doesn’t see the same $PATH as the shell, see this issue. On Unix, running the editor from a shell or changing the .desktop file to set the environment should help.

rustup

rust-analyzer is available in rustup, but only in the nightly toolchain:

1
2
3
---
$ rustup +nightly component add rust-analyzer-preview
---

Arch Linux

The rust-analyzer binary can be installed from the repos or AUR (Arch User Repository):

Install it with pacman, for example:

1
$ pacman -S rust-analyzer

Emacs

Note this excellent guide from @rksm.

Prerequisites: You have installed the rust-analyzer binary.

Emacs support is maintained as part of the Emacs-LSP package in lsp-rust.el.

  1. Install the most recent version of emacs-lsp package by following the Emacs-LSP instructions.

  2. Set lsp-rust-server to 'rust-analyzer.

  3. Run lsp in a Rust buffer.

  4. (Optionally) bind commands like lsp-rust-analyzer-join-lines, lsp-extend-selection and lsp-rust-analyzer-expand-macro to keys.

Vim/NeoVim

Prerequisites: You have installed the rust-analyzer binary. Not needed if the extension can install/update it on its own, coc-rust-analyzer is one example.

The are several LSP client implementations for vim or neovim:

coc-rust-analyzer

  1. Install coc.nvim by following the instructions at coc.nvim (Node.js required)

  2. Run :CocInstall coc-rust-analyzer to install coc-rust-analyzer, this extension implements most of the features supported in the VSCode extension:

    • automatically install and upgrade stable/nightly releases

    • same configurations as VSCode extension, rust-analyzer.server.path, rust-analyzer.cargo.features etc.

    • same commands too, rust-analyzer.analyzerStatus, rust-analyzer.ssr etc.

    • inlay hints for variables and method chaining, Neovim Only

    • semantic highlighting is not implemented yet

Note: for code actions, use coc-codeaction-cursor and coc-codeaction-selected; coc-codeaction and coc-codeaction-line are unlikely to be useful.

LanguageClient-neovim

  1. Install LanguageClient-neovim by following the instructions here

    • The GitHub project wiki has extra tips on configuration

  2. Configure by adding this to your vim/neovim config file (replacing the existing Rust-specific line if it exists):

    1
    2
    3
    
    let g:LanguageClient_serverCommands = {
    \ 'rust': ['rust-analyzer'],
    \ }
    

YouCompleteMe

Install YouCompleteMe by following the instructions here.

rust-analyzer is the default in ycm, it should work out of the box.

ALE

To use the LSP server in ale:

1
let g:ale_linters = {'rust': ['analyzer']}

nvim-lsp

NeoVim 0.5 (not yet released) has built-in language server support. For a quick start configuration of rust-analyzer, use neovim/nvim-lspconfig. Once neovim/nvim-lspconfig is installed, use lua require'lspconfig'.rust_analyzer.setup({}) in your init.vim.

You can also pass LSP settings to the server:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
lua << EOF
local nvim_lsp = require'lspconfig'

local on_attach = function(client)
    require'completion'.on_attach(client)
end

nvim_lsp.rust_analyzer.setup({
    on_attach=on_attach,
    settings = {
        ["rust-analyzer"] = {
            assist = {
                importMergeBehavior = "last",
                importPrefix = "by_self",
            },
            cargo = {
                loadOutDirsFromCheck = true
            },
            procMacro = {
                enable = true
            },
        }
    }
})
EOF

See https://sharksforarms.dev/posts/neovim-rust/ for more tips on getting started.

vim-lsp

vim-lsp is installed by following the plugin instructions. It can be as simple as adding this line to your .vimrc:

1
Plug 'prabirshrestha/vim-lsp'

Next you need to register the rust-analyzer binary. If it is available in $PATH, you may want to add this to your .vimrc:

1
2
3
4
5
6
7
if executable('rust-analyzer')
  au User lsp_setup call lsp#register_server({
        \   'name': 'Rust Language Server',
        \   'cmd': {server_info->['rust-analyzer']},
        \   'whitelist': ['rust'],
        \ })
endif

There is no dedicated UI for the server configuration, so you would need to send any options as a value of the initialization_options field, as described in the Configuration section. Here is an example of how to enable the proc-macro support:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
if executable('rust-analyzer')
  au User lsp_setup call lsp#register_server({
        \   'name': 'Rust Language Server',
        \   'cmd': {server_info->['rust-analyzer']},
        \   'whitelist': ['rust'],
        \   'initialization_options': {
        \     'cargo': {
        \       'loadOutDirsFromCheck': v:true,
        \     },
        \     'procMacro': {
        \       'enable': v:true,
        \     },
        \   },
        \ })
endif

Sublime Text 3

Prerequisites: You have installed the rust-analyzer binary.

You also need the LSP package. To install it:

  1. If you’ve never installed a Sublime Text package, install Package Control:

    • Open the command palette (Win/Linux: ctrl+shift+p, Mac: cmd+shift+p)

    • Type Install Package Control, press enter

  2. In the command palette, run Package control: Install package, and in the list that pops up, type LSP and press enter.

Finally, with your Rust project open, in the command palette, run LSP: Enable Language Server In Project or LSP: Enable Language Server Globally, then select rust-analyzer in the list that pops up to enable the rust-analyzer LSP. The latter means that rust-analyzer is enabled by default in Rust projects.

If it worked, you should see "rust-analyzer, Line X, Column Y" on the left side of the bottom bar, and after waiting a bit, functionality like tooltips on hovering over variables should become available.

If you get an error saying No such file or directory: 'rust-analyzer', see the rust-analyzer binary section on installing the language server binary.

GNOME Builder

GNOME Builder 3.37.1 and newer has native rust-analyzer support. If the LSP binary is not available, GNOME Builder can install it when opening a Rust file.

Eclipse IDE

Support for Rust development in the Eclipse IDE is provided by Eclipse Corrosion. If available in PATH or in some standard location, rust-analyzer is detected and powers editing of Rust files without further configuration. If rust-analyzer is not detected, Corrosion will prompt you for configuration of your Rust toolchain and language server with a link to the Window > Preferences > Rust preference page; from here a button allows to download and configure rust-analyzer, but you can also reference another installation. You’ll need to close and reopen all .rs and Cargo files, or to restart the IDE, for this change to take effect.

Configuration

Source: config.rs

The Installation section contains details on configuration for some of the editors. In general rust-analyzer is configured via LSP messages, which means that it’s up to the editor to decide on the exact format and location of configuration files.

Some clients, such as VS Code or COC plugin in Vim provide rust-analyzer specific configuration UIs. Others may require you to know a bit more about the interaction with rust-analyzer.

For the later category, it might help to know that the initial configuration is specified as a value of the initializationOptions field of the InitializeParams message, in the LSP protocol. The spec says that the field type is any?, but rust-analyzer is looking for a JSON object that is constructed using settings from the list below. Name of the setting, ignoring the rust-analyzer. prefix, is used as a path, and value of the setting becomes the JSON property value.

For example, a very common configuration is to enable proc-macro support, can be achieved by sending this JSON:

1
2
3
4
5
6
7
8
{
  "cargo": {
    "loadOutDirsFromCheck": true,
  },
  "procMacro": {
    "enable": true,
  }
}

Please consult your editor’s documentation to learn more about how to configure LSP servers.

To verify which configuration is actually used by rust-analyzer, set RA_LOG environment variable to rust_analyzer=info and look for config-related messages. Logs should show both the JSON that rust-analyzer sees as well as the updated config.

This is the list of config options rust-analyzer supports:

rust-analyzer.assist.importMergeBehavior (default: "full")

The strategy to use when inserting new imports or merging imports.

rust-analyzer.assist.importPrefix (default: "plain")

The path structure for newly inserted paths to use.

rust-analyzer.assist.importGroup (default: true)

Group inserted imports by the [following order](https://rust-analyzer.github.io/manual.html#auto-import). Groups are separated by newlines.

rust-analyzer.callInfo.full (default: true)

Show function name and docs in parameter hints.

rust-analyzer.cargo.autoreload (default: true)

Automatically refresh project info via cargo metadata on Cargo.toml changes.

rust-analyzer.cargo.allFeatures (default: false)

Activate all available features (--all-features).

rust-analyzer.cargo.features (default: [])

List of features to activate.

rust-analyzer.cargo.runBuildScripts (default: true)

Run build scripts (build.rs) for more precise code analysis.

rust-analyzer.cargo.useRustcWrapperForBuildScripts (default: true)

Use RUSTC_WRAPPER=rust-analyzer when running build scripts to avoid compiling unnecessary things.

rust-analyzer.cargo.noDefaultFeatures (default: false)

Do not activate the default feature.

rust-analyzer.cargo.target (default: null)

Compilation target (target triple).

rust-analyzer.cargo.noSysroot (default: false)

Internal config for debugging, disables loading of sysroot crates.

rust-analyzer.checkOnSave.enable (default: true)

Run specified cargo check command for diagnostics on save.

rust-analyzer.checkOnSave.allFeatures (default: null)

Check with all features (--all-features). Defaults to rust-analyzer.cargo.allFeatures.

rust-analyzer.checkOnSave.allTargets (default: true)

Check all targets and tests (--all-targets).

rust-analyzer.checkOnSave.command (default: "check")

Cargo command to use for cargo check.

rust-analyzer.checkOnSave.noDefaultFeatures (default: null)

Do not activate the default feature.

rust-analyzer.checkOnSave.target (default: null)

Check for a specific target. Defaults to rust-analyzer.cargo.target.

rust-analyzer.checkOnSave.extraArgs (default: [])

Extra arguments for cargo check.

rust-analyzer.checkOnSave.features (default: null)

List of features to activate. Defaults to rust-analyzer.cargo.features.

rust-analyzer.checkOnSave.overrideCommand (default: null)

Advanced option, fully override the command rust-analyzer uses for checking. The command should include --message-format=json or similar option.

rust-analyzer.completion.addCallArgumentSnippets (default: true)

Whether to add argument snippets when completing functions.

rust-analyzer.completion.addCallParenthesis (default: true)

Whether to add parenthesis when completing functions.

rust-analyzer.completion.postfix.enable (default: true)

Whether to show postfix snippets like dbg, if, not, etc.

rust-analyzer.completion.autoimport.enable (default: true)

Toggles the additional completions that automatically add imports when completed. Note that your client must specify the additionalTextEdits LSP client capability to truly have this feature enabled.

rust-analyzer.diagnostics.enable (default: true)

Whether to show native rust-analyzer diagnostics.

rust-analyzer.diagnostics.enableExperimental (default: true)

Whether to show experimental rust-analyzer diagnostics that might have more false positives than usual.

rust-analyzer.diagnostics.disabled (default: [])

List of rust-analyzer diagnostics to disable.

rust-analyzer.diagnostics.remapPrefix (default: {})

Map of prefixes to be substituted when parsing diagnostic file paths. This should be the reverse mapping of what is passed to rustc as --remap-path-prefix.

rust-analyzer.diagnostics.warningsAsHint (default: [])

List of warnings that should be displayed with info severity.

The warnings will be indicated by a blue squiggly underline in code and a blue icon in the Problems Panel.

rust-analyzer.diagnostics.warningsAsInfo (default: [])

List of warnings that should be displayed with hint severity.

The warnings will be indicated by faded text or three dots in code and will not show up in the Problems Panel.

rust-analyzer.files.watcher (default: "client")

Controls file watching implementation.

rust-analyzer.files.excludeDirs (default: [])

These directories will be ignored by rust-analyzer.

rust-analyzer.hoverActions.debug (default: true)

Whether to show Debug action. Only applies when rust-analyzer.hoverActions.enable is set.

rust-analyzer.hoverActions.enable (default: true)

Whether to show HoverActions in Rust files.

rust-analyzer.hoverActions.gotoTypeDef (default: true)

Whether to show Go to Type Definition action. Only applies when rust-analyzer.hoverActions.enable is set.

rust-analyzer.hoverActions.implementations (default: true)

Whether to show Implementations action. Only applies when rust-analyzer.hoverActions.enable is set.

rust-analyzer.hoverActions.run (default: true)

Whether to show Run action. Only applies when rust-analyzer.hoverActions.enable is set.

rust-analyzer.hoverActions.linksInHover (default: true)

Use markdown syntax for links in hover.

rust-analyzer.inlayHints.chainingHints (default: true)

Whether to show inlay type hints for method chains.

rust-analyzer.inlayHints.maxLength (default: 25)

Maximum length for inlay hints. Set to null to have an unlimited length.

rust-analyzer.inlayHints.parameterHints (default: true)

Whether to show function parameter name inlay hints at the call site.

rust-analyzer.inlayHints.typeHints (default: true)

Whether to show inlay type hints for variables.

rust-analyzer.lens.debug (default: true)

Whether to show Debug lens. Only applies when rust-analyzer.lens.enable is set.

rust-analyzer.lens.enable (default: true)

Whether to show CodeLens in Rust files.

rust-analyzer.lens.implementations (default: true)

Whether to show Implementations lens. Only applies when rust-analyzer.lens.enable is set.

rust-analyzer.lens.run (default: true)

Whether to show Run lens. Only applies when rust-analyzer.lens.enable is set.

rust-analyzer.lens.methodReferences (default: false)

Whether to show Method References lens. Only applies when rust-analyzer.lens.enable is set.

rust-analyzer.lens.references (default: false)

Whether to show References lens. Only applies when rust-analyzer.lens.enable is set.

rust-analyzer.linkedProjects (default: [])

Disable project auto-discovery in favor of explicitly specified set of projects.

Elements must be paths pointing to Cargo.toml, rust-project.json, or JSON objects in rust-project.json format.

rust-analyzer.lruCapacity (default: null)

Number of syntax trees rust-analyzer keeps in memory. Defaults to 128.

rust-analyzer.notifications.cargoTomlNotFound (default: true)

Whether to show can’t find Cargo.toml error message.

rust-analyzer.procMacro.enable (default: true)

Enable support for procedural macros, implies rust-analyzer.cargo.runBuildScripts.

rust-analyzer.procMacro.server (default: null)

Internal config, path to proc-macro server executable (typically, this is rust-analyzer itself, but we override this in tests).

rust-analyzer.runnables.overrideCargo (default: null)

Command to be executed instead of 'cargo' for runnables.

rust-analyzer.runnables.cargoExtraArgs (default: [])

Additional arguments to be passed to cargo for runnables such as tests or binaries. For example, it may be --release.

rust-analyzer.rustcSource (default: null)

Path to the Cargo.toml of the rust compiler workspace, for usage in rustc_private projects, or "discover" to try to automatically find it.

Any project which uses rust-analyzer with the rustcPrivate crates must set [package.metadata.rust-analyzer] rustc_private=true to use it.

This option is not reloaded automatically; you must restart rust-analyzer for it to take effect.

rust-analyzer.rustfmt.extraArgs (default: [])

Additional arguments to rustfmt.

rust-analyzer.rustfmt.overrideCommand (default: null)

Advanced option, fully override the command rust-analyzer uses for formatting.

Non-Cargo Based Projects

rust-analyzer does not require Cargo. However, if you use some other build system, you’ll have to describe the structure of your project for rust-analyzer in the rust-project.json format:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
interface JsonProject {
    /// Path to the directory with *source code* of
    /// sysroot crates.
    ///
    /// It should point to the directory where std,
    /// core, and friends can be found:
    ///
    /// https://github.com/rust-lang/rust/tree/master/library.
    ///
    /// If provided, rust-analyzer automatically adds
    /// dependencies on sysroot crates. Conversely,
    /// if you omit this path, you can specify sysroot
    /// dependencies yourself and, for example, have
    /// several different "sysroots" in one graph of
    /// crates.
    sysroot_src?: string;
    /// The set of crates comprising the current
    /// project. Must include all transitive
    /// dependencies as well as sysroot crate (libstd,
    /// libcore and such).
    crates: Crate[];
}

interface Crate {
    /// Optional crate name used for display purposes,
    /// without affecting semantics. See the `deps`
    /// key for semantically-significant crate names.
    display_name?: string;
    /// Path to the root module of the crate.
    root_module: string;
    /// Edition of the crate.
    edition: "2015" | "2018" | "2021";
    /// Dependencies
    deps: Dep[];
    /// Should this crate be treated as a member of
    /// current "workspace".
    ///
    /// By default, inferred from the `root_module`
    /// (members are the crates which reside inside
    /// the directory opened in the editor).
    ///
    /// Set this to `false` for things like standard
    /// library and 3rd party crates to enable
    /// performance optimizations (rust-analyzer
    /// assumes that non-member crates don't change).
    is_workspace_member?: boolean;
    /// Optionally specify the (super)set of `.rs`
    /// files comprising this crate.
    ///
    /// By default, rust-analyzer assumes that only
    /// files under `root_module.parent` can belong
    /// to a crate. `include_dirs` are included
    /// recursively, unless a subdirectory is in
    /// `exclude_dirs`.
    ///
    /// Different crates can share the same `source`.
    ///
    /// If two crates share an `.rs` file in common,
    /// they *must* have the same `source`.
    /// rust-analyzer assumes that files from one
    /// source can't refer to files in another source.
    source?: {
        include_dirs: string[],
        exclude_dirs: string[],
    },
    /// The set of cfgs activated for a given crate, like
    /// `["unix", "feature=\"foo\"", "feature=\"bar\""]`.
    cfg: string[];
    /// Target triple for this Crate.
    ///
    /// Used when running `rustc --print cfg`
    /// to get target-specific cfgs.
    target?: string;
    /// Environment variables, used for
    /// the `env!` macro
    env: : { [key: string]: string; },

    /// For proc-macro crates, path to compiled
    /// proc-macro (.so file).
    proc_macro_dylib_path?: string;
}

interface Dep {
    /// Index of a crate in the `crates` array.
    crate: number,
    /// Name as should appear in the (implicit)
    /// `extern crate name` declaration.
    name: string,
}

This format is provisional and subject to change. Specifically, the roots setup will be different eventually.

There are tree ways to feed rust-project.json to rust-analyzer:

  • Place rust-project.json file at the root of the project, and rust-anlayzer will discover it.

  • Specify "rust-analyzer.linkedProjects": [ "path/to/rust-project.json" ] in the settings (and make sure that your LSP client sends settings as a part of initialize request).

  • Specify "rust-analyzer.linkedProjects": [ { "roots": […​], "crates": […​] }] inline.

Relative paths are interpreted relative to rust-project.json file location or (for inline JSON) relative to rootUri.

You can set RA_LOG environmental variable to rust_analyzer=info to inspect how rust-analyzer handles config and project loading.

Security

At the moment, rust-analyzer assumes that all code is trusted. Here is a non-exhaustive list of ways to make rust-analyzer execute arbitrary code:

  • proc macros and build scripts are executed by default

  • .cargo/config can override rustc with an arbitrary executable

  • VS Code plugin reads configuration from project directory, and that can be used to override paths to various executables, like rustfmt or rust-analyzer itself.

  • rust-analyzer’s syntax trees library uses a lot of unsafe and hasn’t been properly audited for memory safety.

rust-analyzer itself doesn’t access the network. The VS Code plugin doesn’t access the network unless the nightly channel is selected in the settings. In that case, the plugin uses the GitHub API to check for and download updates.

Features

Annotations

Source: annotations.rs

Provides user with annotations above items for looking up references or impl blocks and running/debugging binaries.

113020672 b7c34f00 917a 11eb 8f6e 858735660a0e

Auto Import

Source: auto_import.rs

Using the auto-import assist it is possible to insert missing imports for unresolved items. When inserting an import it will do so in a structured manner by keeping imports grouped, separated by a newline in the following order:

  • std and core

  • External Crates

  • Current Crate, paths prefixed by crate

  • Current Module, paths prefixed by self

  • Super Module, paths prefixed by super

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
use std::fs::File;

use itertools::Itertools;
use syntax::ast;

use crate::utils::insert_use;

use self::auto_import;

use super::AssistContext;
Merge Behavior

It is possible to configure how use-trees are merged with the importMergeBehavior setting. It has the following configurations:

  • full: This setting will cause auto-import to always completely merge use-trees that share the same path prefix while also merging inner trees that share the same path-prefix. This kind of nesting is only supported in Rust versions later than 1.24.

  • last: This setting will cause auto-import to merge use-trees as long as the resulting tree will only contain a nesting of single segment paths at the very end.

  • none: This setting will cause auto-import to never merge use-trees keeping them as simple paths.

In VS Code the configuration for this is rust-analyzer.assist.importMergeBehavior.

Import Prefix

The style of imports in the same crate is configurable through the importPrefix setting. It has the following configurations:

  • by_crate: This setting will force paths to be always absolute, starting with the crate prefix, unless the item is defined outside of the current crate.

  • by_self: This setting will force paths that are relative to the current module to always start with self. This will result in paths that always start with either crate, self, super or an extern crate identifier.

  • plain: This setting does not impose any restrictions in imports.

In VS Code the configuration for this is rust-analyzer.assist.importPrefix.

113020673 b85be580 917a 11eb 9022 59585f35d4f8

Expand Macro Recursively

Source: expand_macro.rs

Shows the full macro expansion of the macro at current cursor.

Editor Action Name

VS Code

Rust Analyzer: Expand macro recursively

113020648 b3973180 917a 11eb 84a9 ecb921293dc5

Expand and Shrink Selection

Extends or shrinks the current selection to the encompassing syntactic construct (expression, statement, item, module, etc). It works with multiple cursors.

This is a standard LSP feature and not a protocol extension.

Editor Shortcut

VS Code

Alt+Shift+β†’, Alt+Shift+←

113020651 b42fc800 917a 11eb 8a4f cf1a07859fac

File Structure

Provides a tree of the symbols defined in the file. Can be used to

  • fuzzy search symbol in a file (super useful)

  • draw breadcrumbs to describe the context around the cursor

  • draw outline of the file

Editor Shortcut

VS Code

Ctrl+Shift+O

113020654 b42fc800 917a 11eb 8388 e7dc4d92b02e

Find All References

Source: references.rs

Shows all references of the item at the cursor location

Editor Shortcut

VS Code

Shift+Alt+F12

113020670 b7c34f00 917a 11eb 8003 370ac5f2b3cb

Format String Completion

Source: format_like.rs

"Result {result} is {2 + 2}" is expanded to the "Result {} is {}", result, 2 + 2.

The following postfix snippets are available:

  • formatformat!(…​)

  • panicpanic!(…​)

  • printlnprintln!(…​)

  • log: + logdlog::debug!(…​) + logtlog::trace!(…​) + logilog::info!(…​) + logwlog::warn!(…​) + logelog::error!(…​)

113020656 b560f500 917a 11eb 87de 02991f61beb8

Go to Definition

Navigates to the definition of an identifier.

Editor Shortcut

VS Code

F12

113065563 025fbe00 91b1 11eb 83e4 a5a703610b23

Go to Implementation

Navigates to the impl block of structs, enums or traits. Also implemented as a code lens.

Editor Shortcut

VS Code

Ctrl+F12

113065566 02f85480 91b1 11eb 9288 aaad8abd8841

Go to Type Definition

Navigates to the type of an identifier.

Editor Action Name

VS Code

*Go to Type Definition

113020657 b560f500 917a 11eb 9007 0f809733a338

Hover

Source: hover.rs

Shows additional information, like type of an expression or documentation for definition when "focusing" code. Focusing is usually hovering with a mouse, but can also be triggered with a shortcut.

113020658 b5f98b80 917a 11eb 9f88 3dbc27320c95

Inlay Hints

Source: inlay_hints.rs

rust-analyzer shows additional information inline with the source code. Editors usually render this using read-only virtual text snippets interspersed with code.

rust-analyzer shows hints for

  • types of local variables

  • names of function arguments

  • types of chained expressions

Note: VS Code does not have native support for inlay hints yet and the hints are implemented using decorations. This approach has limitations, the caret movement and bracket highlighting near the edges of the hint may be weird: 1, 2.

Editor Action Name

VS Code

*Rust Analyzer: Toggle inlay hints

113020660 b5f98b80 917a 11eb 8d70 3be3fd558cdd

Join Lines

Source: join_lines.rs

Join selected lines into one, smartly fixing up whitespace, trailing commas, and braces.

Editor Action Name

VS Code

Rust Analyzer: Join lines

113020661 b6922200 917a 11eb 87c4 b75acc028f11

Magic Completions

Source: lib.rs

In addition to usual reference completion, rust-analyzer provides some ✨magic✨ completions as well:

Keywords like if, else while, loop are completed with braces, and cursor is placed at the appropriate position. Even though if is easy to type, you still want to complete it, to get ` { }` for free! return is inserted with a space or ; depending on the return type of the function.

When completing a function call, () are automatically inserted. If a function takes arguments, the cursor is positioned inside the parenthesis.

There are postfix completions, which can be triggered by typing something like foo().if. The word after . determines postfix completion. Possible variants are:

  • expr.ifif expr {} or if let …​ {} for Option or Result

  • expr.matchmatch expr {}

  • expr.whilewhile expr {} or while let …​ {} for Option or Result

  • expr.ref&expr

  • expr.refm&mut expr

  • expr.letlet $0 = expr;

  • expr.letmlet mut $0 = expr;

  • expr.not!expr

  • expr.dbgdbg!(expr)

  • expr.dbgrdbg!(&expr)

  • expr.call(expr)

There also snippet completions:

Expressions
  • pdeprintln!(" = {:?}", );

  • ppdeprintln!(" = {:#?}", );

Items
  • tfn#[test] fn feature(){}

  • tmod

1
2
3
4
5
6
7
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_name() {}
}

And the auto import completions, enabled with the rust-analyzer.completion.autoimport.enable setting and the corresponding LSP client capabilities. Those are the additional completion options with automatic use import and options from all project importable items, fuzzy matched agains the completion imput.

113020667 b72ab880 917a 11eb 8778 716cf26a0eb3

Matching Brace

If the cursor is on any brace (<>(){}[]||) which is a part of a brace-pair, moves cursor to the matching brace. It uses the actual parser to determine braces, so it won’t confuse generics with comparisons.

Editor Action Name

VS Code

Rust Analyzer: Find matching brace

113065573 04298180 91b1 11eb 8dec d4e2a202f304

Memory Usage

Source: apply_change.rs

Clears rust-analyzer’s internal database and prints memory usage statistics.

Editor Action Name

VS Code

Rust Analyzer: Memory Usage (Clears Database)

113065592 08559f00 91b1 11eb 8c96 64b88068ec02

Move Item

Source: move_item.rs

Move item under cursor or selection up and down.

Editor Action Name

VS Code

Rust Analyzer: Move item up

VS Code

Rust Analyzer: Move item down

113065576 04298180 91b1 11eb 91ce 4505e99ed598

On Enter

Source: on_enter.rs

rust-analyzer can override Enter key to make it smarter:

  • Enter inside triple-slash comments automatically inserts ///

  • Enter in the middle or after a trailing space in // inserts //

  • Enter inside //! doc comments automatically inserts //!

  • Enter after { indents contents and closing } of single-line block

This action needs to be assigned to shortcut explicitly.

VS Code

Add the following to keybindings.json:

1
2
3
4
5
{
  "key": "Enter",
  "command": "rust-analyzer.onEnter",
  "when": "editorTextFocus && !suggestWidgetVisible && editorLangId == rust"
}
113065578 04c21800 91b1 11eb 82b8 22b8c481e645

On Typing Assists

Source: typing.rs

Some features trigger on typing certain characters:

  • typing let = tries to smartly add ; if = is followed by an existing expression

  • typing . in a chain method call auto-indents

  • typing { in front of an expression inserts a closing } after the expression

    VS Code

    Add the following to settings.json:

"editor.formatOnType": true,
113166163 69758500 923a 11eb 81ee eb33ec380399
113171066 105c2000 923f 11eb 87ab f4a263346567

Parent Module

Navigates to the parent module of the current module.

Editor Action Name

VS Code

Rust Analyzer: Locate parent module

113065580 04c21800 91b1 11eb 9a32 00086161c0bd

Source: runnables.rs

Provides a sneak peek of all tests where the current item is used.

The simplest way to use this feature is via the context menu: - Right-click on the selected item. The context menu opens. - Select Peek related tests

Editor Action Name

VS Code

Rust Analyzer: Peek related tests

Rename

Source: rename.rs

Renames the item below the cursor and all of its references

Editor Shortcut

VS Code

F2

113065582 055aae80 91b1 11eb 8ade 2b58e6d81883

Run

Source: runnables.rs

Shows a popup suggesting to run a test/benchmark/binary at the current cursor location. Super useful for repeatedly running just a single test. Do bind this to a shortcut!

Editor Action Name

VS Code

Rust Analyzer: Run

113065583 055aae80 91b1 11eb 958f d67efcaf6a2f

Semantic Syntax Highlighting

rust-analyzer highlights the code semantically. For example, bar in foo::Bar might be colored differently depending on whether Bar is an enum or a trait. rust-analyzer does not specify colors directly, instead it assigns tag (like struct) and a set of modifiers (like declaration) to each token. It’s up to the client to map those to specific colors.

The general rule is that a reference to an entity gets colored the same way as the entity itself. We also give special modifier for mut and &mut local variables.

113164457 06cfb980 9239 11eb 819b 0f93e646acf8
113187625 f7f50100 9250 11eb 825e 91c58f236071

Show Syntax Tree

Source: syntax_tree.rs

Shows the parse tree of the current file. It exists mostly for debugging rust-analyzer itself.

Editor Action Name

VS Code

Rust Analyzer: Show Syntax Tree

113065586 068bdb80 91b1 11eb 9507 fee67f9f45a0

Status

Source: status.rs

Shows internal statistic about memory usage of rust-analyzer.

Editor Action Name

VS Code

Rust Analyzer: Status

113065584 05f34500 91b1 11eb 98cc 5c196f76be7f

Structural Search and Replace

Source: lib.rs

Search and replace with named wildcards that will match any expression, type, path, pattern or item. The syntax for a structural search replace command is <search_pattern> =⇒> <replace_pattern>. A $<name> placeholder in the search pattern will match any AST node and $<name> will reference it in the replacement. Within a macro call, a placeholder will match up until whatever token follows the placeholder.

All paths in both the search pattern and the replacement template must resolve in the context in which this command is invoked. Paths in the search pattern will then match the code if they resolve to the same item, even if they’re written differently. For example if we invoke the command in the module foo with a pattern of Bar, then code in the parent module that refers to foo::Bar will match.

Paths in the replacement template will be rendered appropriately for the context in which the replacement occurs. For example if our replacement template is foo::Bar and we match some code in the foo module, we’ll insert just Bar.

Inherent method calls should generally be written in UFCS form. e.g. foo::Bar::baz($s, $a) will match $s.baz($a), provided the method call baz resolves to the method foo::Bar::baz. When a placeholder is the receiver of a method call in the search pattern (e.g. $s.foo()), but not in the replacement template (e.g. bar($s)), then *, & and &mut will be added as needed to mirror whatever autoderef and autoref was happening implicitly in the matched code.

The scope of the search / replace will be restricted to the current selection if any, otherwise it will apply to the whole workspace.

Placeholders may be given constraints by writing them as ${<name>:<constraint1>:<constraint2>…​}.

Supported constraints:

Constraint Restricts placeholder

kind(literal)

Is a literal (e.g. 42 or "forty two")

not(a)

Negates the constraint a

Available via the command rust-analyzer.ssr.

1
2
3
4
5
6
7
// Using structural search replace command [foo($a, $b) ==>> ($a).foo($b)]

// BEFORE
String::from(foo(y + 5, z))

// AFTER
String::from((y + 5).foo(z))
Editor Action Name

VS Code

Rust Analyzer: Structural Search Replace

Also available as an assist, by writing a comment containing the structural search and replace rule. You will only see the assist if the comment can be parsed as a valid structural search and replace rule.

1
2
// Place the cursor on the line below to see the assist πŸ’‘.
// foo($a, $b) ==>> ($a).foo($b)

View Hir

Source: view_hir.rs

Editor Action Name

VS Code

Rust Analyzer: View Hir

113065588 068bdb80 91b1 11eb 9a78 0b4ef1e972fb

Workspace Symbol

Source: symbol_index.rs

Uses fuzzy-search to find types, modules and functions by name across your project and dependencies. This is the most useful feature, which improves code navigation tremendously. It mostly works on top of the built-in LSP functionality, however # and * symbols can be used to narrow down the search. Specifically,

  • Foo searches for Foo type in the current workspace

  • foo# searches for foo function in the current workspace

  • Foo* searches for Foo type among dependencies, including stdlib

  • foo#* searches for foo function among dependencies

That is, # switches from "types" to all symbols, * switches from the current workspace to dependencies.

Editor Shortcut

VS Code

Ctrl+T

Assists (Code Actions)

Assists, or code actions, are small local refactorings, available in a particular context. They are usually triggered by a shortcut or by clicking a light bulb icon in the editor. Cursor position or selection is signified by ┃ character.

add_explicit_type

Specify type for a let binding.

Before
1
2
3
fn main() {
    let x┃ = 92;
}
After
1
2
3
fn main() {
    let x: i32 = 92;
}

add_hash

Source: raw_string.rs

Adds a hash to a raw string literal.

Before
1
2
3
fn main() {
    r#"Hello,┃ World!"#;
}
After
1
2
3
fn main() {
    r##"Hello, World!"##;
}

add_impl_default_members

Adds scaffold for overriding default impl members.

Before
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
trait Trait {
    type X;
    fn foo(&self);
    fn bar(&self) {}
}

impl Trait for () {
    type X = ();
    fn foo(&self) {}┃

}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
trait Trait {
    type X;
    fn foo(&self);
    fn bar(&self) {}
}

impl Trait for () {
    type X = ();
    fn foo(&self) {}

    ┃fn bar(&self) {}
}

add_impl_missing_members

Adds scaffold for required impl members.

Before
1
2
3
4
5
6
7
8
9
trait Trait<T> {
    type X;
    fn foo(&self) -> T;
    fn bar(&self) {}
}

impl Trait<u32> for () {┃

}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
trait Trait<T> {
    type X;
    fn foo(&self) -> T;
    fn bar(&self) {}
}

impl Trait<u32> for () {
    ┃type X;

    fn foo(&self) -> u32 {
        todo!()
    }
}

add_lifetime_to_type

Adds a new lifetime to a struct, enum or union.

Before
1
2
3
4
struct Point {
    x: &┃u32,
    y: u32,
}
After
1
2
3
4
struct Point<'a> {
    x: &'a u32,
    y: u32,
}

add_turbo_fish

Adds ::<_> to a call of a generic method or function.

Before
1
2
3
4
fn make<T>() -> T { todo!() }
fn main() {
    let x = make┃();
}
After
1
2
3
4
fn make<T>() -> T { todo!() }
fn main() {
    let x = make::<${0:_}>();
}

apply_demorgan

Apply De Morgan’s law. This transforms expressions of the form !l || !r into !(l && r). This also works with &&. This assist can only be applied with the cursor on either || or &&.

Before
1
2
3
fn main() {
    if x != 4 ||┃ y < 3.14 {}
}
After
1
2
3
fn main() {
    if !(x == 4 && !(y < 3.14)) {}
}

auto_import

Source: auto_import.rs

If the name is unresolved, provides all possible imports for it.

Before
1
2
3
fn main() {
    let map = HashMap┃::new();
}
After
1
2
3
4
5
use std::collections::HashMap;

fn main() {
    let map = HashMap::new();
}

change_visibility

Adds or changes existing visibility specifier.

Before
1
┃fn frobnicate() {}
After
1
pub(crate) fn frobnicate() {}

convert_integer_literal

Converts the base of integer literals to other bases.

Before
1
const _: i32 = 10┃;
After
1
const _: i32 = 0b1010;

convert_into_to_from

Converts an Into impl to an equivalent From impl.

Before
1
2
3
4
5
6
7
8
impl ┃Into<Thing> for usize {
    fn into(self) -> Thing {
        Thing {
            b: self.to_string(),
            a: self
        }
    }
}
After
1
2
3
4
5
6
7
8
impl From<usize> for Thing {
    fn from(val: usize) -> Self {
        Thing {
            b: val.to_string(),
            a: val
        }
    }
}

convert_iter_for_each_to_for

Converts an Iterator::for_each function into a for loop.

Before
1
2
3
4
5
6
fn main() {
    let iter = SomeIter;
    iter.for_each┃(|(x, y)| {
        println!("x: {}, y: {}", x, y);
    });
}
After
1
2
3
4
5
6
fn main() {
    let iter = SomeIter;
    for (x, y) in iter {
        println!("x: {}, y: {}", x, y);
    }
}

convert_to_guarded_return

Source: early_return.rs

Replace a large conditional with a guarded return.

Before
1
2
3
4
5
6
fn main() {
    ┃if cond {
        foo();
        bar();
    }
}
After
1
2
3
4
5
6
7
fn main() {
    if !cond {
        return;
    }
    foo();
    bar();
}

convert_tuple_struct_to_named_struct

Converts tuple struct to struct with named fields.

Before
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
struct Point┃(f32, f32);

impl Point {
    pub fn new(x: f32, y: f32) -> Self {
        Point(x, y)
    }

    pub fn x(&self) -> f32 {
        self.0
    }

    pub fn y(&self) -> f32 {
        self.1
    }
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
struct Point { field1: f32, field2: f32 }

impl Point {
    pub fn new(x: f32, y: f32) -> Self {
        Point { field1: x, field2: y }
    }

    pub fn x(&self) -> f32 {
        self.field1
    }

    pub fn y(&self) -> f32 {
        self.field2
    }
}

expand_glob_import

Expands glob imports.

Before
1
2
3
4
5
6
7
8
mod foo {
    pub struct Bar;
    pub struct Baz;
}

use foo::*┃;

fn qux(bar: Bar, baz: Baz) {}
After
1
2
3
4
5
6
7
8
mod foo {
    pub struct Bar;
    pub struct Baz;
}

use foo::{Baz, Bar};

fn qux(bar: Bar, baz: Baz) {}

extract_function

Extracts selected statements into new function.

Before
1
2
3
4
5
6
fn main() {
    let n = 1;
    ┃let m = n + 2;
    let k = m + n;┃
    let g = 3;
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
fn main() {
    let n = 1;
    fun_name(n);
    let g = 3;
}

fn ┃fun_name(n: i32) {
    let m = n + 2;
    let k = m + n;
}

extract_struct_from_enum_variant

Extracts a struct from enum variant.

Before
1
enum A { ┃One(u32, u32) }
After
1
2
3
struct One(pub u32, pub u32);

enum A { One(One) }

extract_type_alias

Extracts the selected type as a type alias.

Before
1
2
3
struct S {
    field: ┃(u8, u8, u8)┃,
}
After
1
2
3
4
5
type ┃Type = (u8, u8, u8);

struct S {
    field: Type,
}

extract_variable

Extracts subexpression into a variable.

Before
1
2
3
fn main() {
    ┃(1 + 2)┃ * 4;
}
After
1
2
3
4
fn main() {
    let ┃var_name = (1 + 2);
    var_name * 4;
}

fill_match_arms

Adds missing clauses to a match expression.

Before
1
2
3
4
5
6
7
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        ┃
    }
}
After
1
2
3
4
5
6
7
8
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        ┃Action::Move { distance } => {}
        Action::Stop => {}
    }
}

fix_visibility

Makes inaccessible item public.

Before
1
2
3
4
5
6
mod m {
    fn frobnicate() {}
}
fn main() {
    m::frobnicate┃() {}
}
After
1
2
3
4
5
6
mod m {
    ┃pub(crate) fn frobnicate() {}
}
fn main() {
    m::frobnicate() {}
}

flip_binexpr

Source: flip_binexpr.rs

Flips operands of a binary expression.

Before
1
2
3
fn main() {
    let _ = 90 +┃ 2;
}
After
1
2
3
fn main() {
    let _ = 2 + 90;
}

flip_comma

Source: flip_comma.rs

Flips two comma-separated items.

Before
1
2
3
fn main() {
    ((1, 2),┃ (3, 4));
}
After
1
2
3
fn main() {
    ((3, 4), (1, 2));
}

flip_trait_bound

Flips two trait bounds.

Before
1
fn foo<T: Clone +┃ Copy>() { }
After
1
fn foo<T: Copy + Clone>() { }

generate_default_from_enum_variant

Adds a Default impl for an enum using a variant.

Before
1
2
3
4
5
enum Version {
 Undefined,
 Minor┃,
 Major,
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
enum Version {
 Undefined,
 Minor,
 Major,
}

impl Default for Version {
    fn default() -> Self {
        Self::Minor
    }
}

generate_default_from_new

Generates default implementation from new method.

Before
1
2
3
4
5
6
7
struct Example { _inner: () }

impl Example {
    pub fn n┃ew() -> Self {
        Self { _inner: () }
    }
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
struct Example { _inner: () }

impl Example {
    pub fn new() -> Self {
        Self { _inner: () }
    }
}

impl Default for Example {
    fn default() -> Self {
        Self::new()
    }
}

generate_deref

Generate Deref impl using the given struct field.

Before
1
2
3
4
struct A;
struct B {
   ┃a: A
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
struct A;
struct B {
   a: A
}

impl std::ops::Deref for B {
    type Target = A;

    fn deref(&self) -> &Self::Target {
        &self.a
    }
}

generate_derive

Adds a new #[derive()] clause to a struct or enum.

Before
1
2
3
4
struct Point {
    x: u32,
    y: u32,┃
}
After
1
2
3
4
5
#[derive(┃)]
struct Point {
    x: u32,
    y: u32,
}

generate_enum_as_method

Generate an as_ method for an enum variant.

Before
1
2
3
4
enum Value {
 Number(i32),
 Text(String)┃,
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
enum Value {
 Number(i32),
 Text(String),
}

impl Value {
    fn as_text(&self) -> Option<&String> {
        if let Self::Text(v) = self {
            Some(v)
        } else {
            None
        }
    }
}

generate_enum_is_method

Generate an is_ method for an enum variant.

Before
1
2
3
4
5
enum Version {
 Undefined,
 Minor┃,
 Major,
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
enum Version {
 Undefined,
 Minor,
 Major,
}

impl Version {
    /// Returns `true` if the version is [`Minor`].
    fn is_minor(&self) -> bool {
        matches!(self, Self::Minor)
    }
}

generate_enum_try_into_method

Generate an try_into_ method for an enum variant.

Before
1
2
3
4
enum Value {
 Number(i32),
 Text(String)┃,
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
enum Value {
 Number(i32),
 Text(String),
}

impl Value {
    fn try_into_text(self) -> Result<String, Self> {
        if let Self::Text(v) = self {
            Ok(v)
        } else {
            Err(self)
        }
    }
}

generate_from_impl_for_enum

Adds a From impl for an enum variant with one tuple field.

Before
1
enum A { ┃One(u32) }
After
1
2
3
4
5
6
7
enum A { One(u32) }

impl From<u32> for A {
    fn from(v: u32) -> Self {
        Self::One(v)
    }
}

generate_function

Adds a stub function with a signature matching the function under the cursor.

Before
1
2
3
4
5
struct Baz;
fn baz() -> Baz { Baz }
fn foo() {
    bar┃("", baz());
}
After
1
2
3
4
5
6
7
8
9
struct Baz;
fn baz() -> Baz { Baz }
fn foo() {
    bar("", baz());
}

fn bar(arg: &str, baz: Baz) ${0:-> ()} {
    todo!()
}

generate_getter

Generate a getter method.

Before
1
2
3
struct Person {
    nam┃e: String,
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
struct Person {
    name: String,
}

impl Person {
    /// Get a reference to the person's name.
    fn name(&self) -> &String {
        &self.name
    }
}

generate_getter_mut

Generate a mut getter method.

Before
1
2
3
struct Person {
    nam┃e: String,
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
struct Person {
    name: String,
}

impl Person {
    /// Get a mutable reference to the person's name.
    fn name_mut(&mut self) -> &mut String {
        &mut self.name
    }
}

generate_impl

Adds a new inherent impl for a type.

Before
1
2
3
struct Ctx<T: Clone> {
    data: T,┃
}
After
1
2
3
4
5
6
7
struct Ctx<T: Clone> {
    data: T,
}

impl<T: Clone> Ctx<T> {
    ┃
}

generate_is_empty_from_len

Generates is_empty implementation from the len method.

Before
1
2
3
4
5
6
7
struct MyStruct { data: Vec<String> }

impl MyStruct {
    p┃ub fn len(&self) -> usize {
        self.data.len()
    }
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
struct MyStruct { data: Vec<String> }

impl MyStruct {
    pub fn len(&self) -> usize {
        self.data.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

generate_new

Source: generate_new.rs

Adds a new inherent impl for a type.

Before
1
2
3
struct Ctx<T: Clone> {
     data: T,┃
}
After
1
2
3
4
5
6
7
struct Ctx<T: Clone> {
     data: T,
}

impl<T: Clone> Ctx<T> {
    fn ┃new(data: T) -> Self { Self { data } }
}

generate_setter

Generate a setter method.

Before
1
2
3
struct Person {
    nam┃e: String,
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
struct Person {
    name: String,
}

impl Person {
    /// Set the person's name.
    fn set_name(&mut self, name: String) {
        self.name = name;
    }
}

infer_function_return_type

Adds the return type to a function or closure inferred from its tail expression if it doesn’t have a return type specified. This assists is useable in a functions or closures tail expression or return type position.

Before
1
fn foo() { 4┃2i32 }
After
1
fn foo() -> i32 { 42i32 }

inline_function

Inlines a function body.

Before
1
2
3
4
fn add(a: u32, b: u32) -> u32 { a + b }
fn main() {
    let x = add┃(1, 2);
}
After
1
2
3
4
5
6
7
8
fn add(a: u32, b: u32) -> u32 { a + b }
fn main() {
    let x = {
        let a = 1;
        let b = 2;
        a + b
    };
}

inline_local_variable

Inlines local variable.

Before
1
2
3
4
fn main() {
    let x┃ = 1 + 2;
    x * 4;
}
After
1
2
3
fn main() {
    (1 + 2) * 4;
}

introduce_named_lifetime

Change an anonymous lifetime to a named lifetime.

Before
1
2
3
4
5
6
7
impl Cursor<'_┃> {
    fn node(self) -> &SyntaxNode {
        match self {
            Cursor::Replace(node) | Cursor::Before(node) => node,
        }
    }
}
After
1
2
3
4
5
6
7
impl<'a> Cursor<'a> {
    fn node(self) -> &SyntaxNode {
        match self {
            Cursor::Replace(node) | Cursor::Before(node) => node,
        }
    }
}

invert_if

Source: invert_if.rs

Apply invert_if This transforms if expressions of the form if !x {A} else {B} into if x {B} else {A} This also works with !=. This assist can only be applied with the cursor on if.

Before
1
2
3
fn main() {
    if┃ !y { A } else { B }
}
After
1
2
3
fn main() {
    if y { B } else { A }
}

make_raw_string

Source: raw_string.rs

Adds r# to a plain string literal.

Before
1
2
3
fn main() {
    "Hello,┃ World!";
}
After
1
2
3
fn main() {
    r#"Hello, World!"#;
}

make_usual_string

Source: raw_string.rs

Turns a raw string into a plain string.

Before
1
2
3
fn main() {
    r#"Hello,┃ "World!""#;
}
After
1
2
3
fn main() {
    "Hello, \"World!\"";
}

merge_imports

Merges two imports with a common prefix.

Before
1
2
use std::┃fmt::Formatter;
use std::io;
After
1
use std::{fmt::Formatter, io};

merge_match_arms

Merges identical match arms.

Before
1
2
3
4
5
6
7
8
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        ┃Action::Move(..) => foo(),
        Action::Stop => foo(),
    }
}
After
1
2
3
4
5
6
7
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move(..) | Action::Stop => foo(),
    }
}

move_arm_cond_to_match_guard

Source: move_guard.rs

Moves if expression from match arm body into a guard.

Before
1
2
3
4
5
6
7
8
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move { distance } => ┃if distance > 10 { foo() },
        _ => (),
    }
}
After
1
2
3
4
5
6
7
8
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move { distance } if distance > 10 => foo(),
        _ => (),
    }
}

move_bounds_to_where_clause

Source: move_bounds.rs

Moves inline type bounds to a where clause.

Before
1
2
3
fn apply<T, U, ┃F: FnOnce(T) -> U>(f: F, x: T) -> U {
    f(x)
}
After
1
2
3
fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
    f(x)
}

move_guard_to_arm_body

Source: move_guard.rs

Moves match guard into match arm body.

Before
1
2
3
4
5
6
7
8
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move { distance } ┃if distance > 10 => foo(),
        _ => (),
    }
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move { distance } => if distance > 10 {
            foo()
        },
        _ => (),
    }
}

move_module_to_file

Moves inline module’s contents to a separate file.

Before
1
2
3
mod ┃foo {
    fn t() {}
}
After
1
mod foo;

pull_assignment_up

Extracts variable assignment to outside an if or match statement.

Before
1
2
3
4
5
6
7
8
9
fn main() {
    let mut foo = 6;

    if true {
        ┃foo = 5;
    } else {
        foo = 4;
    }
}
After
1
2
3
4
5
6
7
8
9
fn main() {
    let mut foo = 6;

    foo = if true {
        5
    } else {
        4
    };
}

qualify_path

Source: qualify_path.rs

If the name is unresolved, provides all possible qualified paths for it.

Before
1
2
3
fn main() {
    let map = HashMap┃::new();
}
After
1
2
3
fn main() {
    let map = std::collections::HashMap::new();
}

remove_dbg

Source: remove_dbg.rs

Removes dbg!() macro call.

Before
1
2
3
fn main() {
    ┃dbg!(92);
}
After
1
2
3
fn main() {
    92;
}

remove_hash

Source: raw_string.rs

Removes a hash from a raw string literal.

Before
1
2
3
fn main() {
    r#"Hello,┃ World!"#;
}
After
1
2
3
fn main() {
    r"Hello, World!";
}

remove_mut

Source: remove_mut.rs

Removes the mut keyword.

Before
1
2
3
impl Walrus {
    fn feed(&mut┃ self, amount: u32) {}
}
After
1
2
3
impl Walrus {
    fn feed(&self, amount: u32) {}
}

remove_unused_param

Removes unused function parameter.

Before
1
2
3
4
5
fn frobnicate(x: i32┃) {}

fn main() {
    frobnicate(92);
}
After
1
2
3
4
5
fn frobnicate() {}

fn main() {
    frobnicate();
}

reorder_fields

Reorder the fields of record literals and record patterns in the same order as in the definition.

Before
1
2
struct Foo {foo: i32, bar: i32};
const test: Foo = ┃Foo {bar: 0, foo: 1}
After
1
2
struct Foo {foo: i32, bar: i32};
const test: Foo = Foo {foo: 1, bar: 0}

reorder_impl

Source: reorder_impl.rs

Reorder the methods of an impl Trait. The methods will be ordered in the same order as in the trait definition.

Before
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
trait Foo {
    fn a() {}
    fn b() {}
    fn c() {}
}

struct Bar;
┃impl Foo for Bar {
    fn b() {}
    fn c() {}
    fn a() {}
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
trait Foo {
    fn a() {}
    fn b() {}
    fn c() {}
}

struct Bar;
impl Foo for Bar {
    fn a() {}
    fn b() {}
    fn c() {}
}

replace_derive_with_manual_impl

Converts a derive impl into a manual one.

Before
1
2
#[derive(Deb┃ug, Display)]
struct S;
After
1
2
3
4
5
6
7
8
#[derive(Display)]
struct S;

impl Debug for S {
    fn fmt(&self, f: &mut Formatter) -> Result<()> {
        ${0:todo!()}
    }
}

replace_for_loop_with_for_each

Converts a for loop into a for_each loop on the Iterator.

Before
1
2
3
4
5
6
fn main() {
    let x = vec![1, 2, 3];
    for┃ v in x {
        let y = v * 2;
    }
}
After
1
2
3
4
5
6
fn main() {
    let x = vec![1, 2, 3];
    x.into_iter().for_each(|v| {
        let y = v * 2;
    });
}

replace_if_let_with_match

Replaces if let with an else branch with a match expression.

Before
1
2
3
4
5
6
7
8
9
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    ┃if let Action::Move { distance } = action {
        foo(distance)
    } else {
        bar()
    }
}
After
1
2
3
4
5
6
7
8
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move { distance } => foo(distance),
        _ => bar(),
    }
}

replace_impl_trait_with_generic

Replaces impl Trait function argument with the named generic.

Before
1
fn foo(bar: ┃impl Bar) {}
After
1
fn foo<B: Bar>(bar: B) {}

replace_let_with_if_let

Replaces let with an if-let.

Before
1
2
3
4
5
6
fn main(action: Action) {
    ┃let x = compute();
}

fn compute() -> Option<i32> { None }
After
1
2
3
4
5
6
7
fn main(action: Action) {
    if let Some(x) = compute() {
    }
}

fn compute() -> Option<i32> { None }

replace_match_with_if_let

Replaces a binary match with a wildcard pattern and no guards with an if let expression.

Before
1
2
3
4
5
6
7
8
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    ┃match action {
        Action::Move { distance } => foo(distance),
        _ => bar(),
    }
}
After
1
2
3
4
5
6
7
8
9
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    if let Action::Move { distance } = action {
        foo(distance)
    } else {
        bar()
    }
}

replace_qualified_name_with_use

Adds a use statement for a given fully-qualified name.

Before
1
fn process(map: std::collections::┃HashMap<String, String>) {}
After
1
2
3
use std::collections::HashMap;

fn process(map: HashMap<String, String>) {}

replace_string_with_char

Replace string with char.

Before
1
2
3
fn main() {
    find("{┃");
}
After
1
2
3
fn main() {
    find('{');
}

replace_unwrap_with_match

Replaces unwrap a match expression. Works for Result and Option.

Before
1
2
3
4
5
enum Result<T, E> { Ok(T), Err(E) }
fn main() {
    let x: Result<i32, i32> = Result::Ok(92);
    let y = x.┃unwrap();
}
After
1
2
3
4
5
6
7
8
enum Result<T, E> { Ok(T), Err(E) }
fn main() {
    let x: Result<i32, i32> = Result::Ok(92);
    let y = match x {
        Ok(a) => a,
        ┃_ => unreachable!(),
    };
}

split_import

Source: split_import.rs

Wraps the tail of import into braces.

Before
1
use std::┃collections::HashMap;
After
1
use std::{collections::HashMap};

toggle_ignore

Adds #[ignore] attribute to the test.

Before
1
2
3
4
┃#[test]
fn arithmetics {
    assert_eq!(2 + 2, 5);
}
After
1
2
3
4
5
#[test]
#[ignore]
fn arithmetics {
    assert_eq!(2 + 2, 5);
}

unmerge_use

Source: unmerge_use.rs

Extracts single use item from use list.

Before
1
use std::fmt::{Debug, Display┃};
After
1
2
use std::fmt::{Debug};
use std::fmt::Display;

unwrap_block

Source: unwrap_block.rs

This assist removes if…​else, for, while and loop control statements to just keep the body.

Before
1
2
3
4
5
fn foo() {
    if true {┃
        println!("foo");
    }
}
After
1
2
3
fn foo() {
    println!("foo");
}

wrap_return_type_in_result

Wrap the function’s return type into Result.

Before
1
fn foo() -> i32┃ { 42i32 }
After
1
fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }

Diagnostics

While most errors and warnings provided by rust-analyzer come from the cargo check integration, there’s a growing number of diagnostics implemented using rust-analyzer’s own analysis. Some of these diagnostics don’t respect #[allow] or \#[deny] attributes yet, but can be turned off using the rust-analyzer.diagnostics.enable, rust-analyzer.diagnostics.enableExperimental or rust-analyzer.diagnostics.disabled settings.

break-outside-of-loop

Source: diagnostics.rs

This diagnostic is triggered if the break keyword is used outside of a loop.

inactive-code

Source: diagnostics.rs

This diagnostic is shown for code with inactive #[cfg] attributes.

incorrect-ident-case

Source: diagnostics.rs

This diagnostic is triggered if an item name doesn’t follow Rust naming convention.

macro-error

Source: diagnostics.rs

This diagnostic is shown for macro expansion errors.

mismatched-arg-count

Source: diagnostics.rs

This diagnostic is triggered if a function is invoked with an incorrect amount of arguments.

missing-match-arm

Source: diagnostics.rs

This diagnostic is triggered if match block is missing one or more match arms.

missing-ok-or-some-in-tail-expr

Source: diagnostics.rs

This diagnostic is triggered if a block that should return Result returns a value not wrapped in Ok, or if a block that should return Option returns a value not wrapped in Some.

Example:

1
2
3
fn foo() -> Result<u8, ()> {
    10
}

missing-pat-fields

Source: diagnostics.rs

This diagnostic is triggered if pattern lacks some fields that exist in the corresponding structure.

Example:

1
2
3
4
5
6
7
struct A { a: u8, b: u8 }

let a = A { a: 10, b: 20 };

if let A { a } = a {
    // ...
}

missing-structure-fields

Source: diagnostics.rs

This diagnostic is triggered if record lacks some fields that exist in the corresponding structure.

Example:

1
2
3
struct A { a: u8, b: u8 }

let a = A { a: 10 };

missing-unsafe

Source: diagnostics.rs

This diagnostic is triggered if an operation marked as unsafe is used outside of an unsafe function or block.

no-such-field

Source: diagnostics.rs

This diagnostic is triggered if created structure does not have field provided in record.

replace-filter-map-next-with-find-map

Source: diagnostics.rs

This diagnostic is triggered when .filter_map(..).next() is used, rather than the more concise .find_map(..).

unlinked-file

This diagnostic is shown for files that are not included in any crate, or files that are part of crates rust-analyzer failed to discover. The file will not have IDE features available.

unresolved-extern-crate

Source: diagnostics.rs

This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate.

unresolved-import

Source: diagnostics.rs

This diagnostic is triggered if rust-analyzer is unable to discover imported module.

unresolved-macro-call

Source: diagnostics.rs

This diagnostic is triggered if rust-analyzer is unable to resolve the path to a macro in a macro invocation.

unresolved-module

Source: diagnostics.rs

This diagnostic is triggered if rust-analyzer is unable to discover referred module.

unresolved-proc-macro

Source: diagnostics.rs

This diagnostic is shown when a procedural macro can not be found. This usually means that procedural macro support is simply disabled (and hence is only a weak hint instead of an error), but can also indicate project setup problems.

If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the rust-analyzer.diagnostics.disabled list to prevent them from showing. Alternatively you can enable support for procedural macros (see rust-analyzer.procMacro.enable).

Editor Features

VS Code

Color configurations

It is possible to change the foreground/background color of inlay hints. Just add this to your settings.json:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
  "workbench.colorCustomizations": {
    // Name of the theme you are currently using
    "[Default Dark+]": {
      "rust_analyzer.inlayHints.foreground": "#868686f0",
      "rust_analyzer.inlayHints.background": "#3d3d3d48",

      // Overrides for specific kinds of inlay hints
      "rust_analyzer.inlayHints.foreground.typeHints": "#fdb6fdf0",
      "rust_analyzer.inlayHints.foreground.paramHints": "#fdb6fdf0",
      "rust_analyzer.inlayHints.background.chainingHints": "#6b0c0c81"
    }
  }
}

Semantic style customizations

You can customize the look of different semantic elements in the source code. For example, mutable bindings are underlined by default and you can override this behavior by adding the following section to your settings.json:

1
2
3
4
5
6
7
8
9
{
  "editor.semanticTokenColorCustomizations": {
    "rules": {
      "*.mutable": {
        "fontStyle": "", // underline is the default
      },
    }
  },
}

Special when clause context for keybindings.

You may use inRustProject context to configure keybindings for rust projects only. For example:

1
2
3
4
5
{
  "key": "ctrl+i",
  "command": "rust-analyzer.toggleInlayHints",
  "when": "inRustProject"
}

More about when clause contexts here.

Setting runnable environment variables

You can use "rust-analyzer.runnableEnv" setting to define runnable environment-specific substitution variables. The simplest way for all runnables in a bunch:

1
2
3
"rust-analyzer.runnableEnv": {
    "RUN_SLOW_TESTS": "1"
}

Or it is possible to specify vars more granularly:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
"rust-analyzer.runnableEnv": [
    {
        // "mask": null, // null mask means that this rule will be applied for all runnables
        env: {
             "APP_ID": "1",
             "APP_DATA": "asdf"
        }
    },
    {
        "mask": "test_name",
        "env": {
             "APP_ID": "2", // overwrites only APP_ID
        }
    }
]

You can use any valid regular expression as a mask. Also note that a full runnable name is something like run bin_or_example_name, test some::mod::test_name or test-mod some::mod, so it is possible to distinguish binaries, single tests, and test modules with this masks: "^run", "^test " (the trailing space matters!), and "^test-mod" respectively.

Compiler feedback from external commands

Instead of relying on the built-in cargo check, you can configure Code to run a command in the background and use the $rustc-watch problem matcher to generate inline error markers from its output.

To do this you need to create a new VS Code Task and set rust-analyzer.checkOnSave.enable: false in preferences.

For example, if you want to run cargo watch instead, you might add the following to .vscode/tasks.json:

1
2
3
4
5
6
7
8
{
    "label": "Watch",
    "group": "build",
    "type": "shell",
    "command": "cargo watch",
    "problemMatcher": "$rustc-watch",
    "isBackground": true
}