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.

Table of Contents

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

Toolchain

Only the latest stable standard library source is officially supported for use with rust-analyzer. If you are using an older toolchain or have an override set, rust-analyzer may fail to understand the Rust source. You will either need to update your toolchain or use an older version of rust-analyzer that is compatible with your toolchain.

If you are using an override in your project, you can still force rust-analyzer to use the stable toolchain via the environment variable RUSTUP_TOOLCHAIN. For example, with VS Code or coc-rust-analyzer:

1
{ "rust-analyzer.server.extraEnv": { "RUSTUP_TOOLCHAIN": "stable" } }

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 previous official Rust plugin. The latter is no longer maintained and should be uninstalled.

The server binary is stored in the extension install directory, which starts with rust-lang.rust-analyzer- and is located under:

  • Linux: ~/.vscode/extensions

  • Linux (Remote, such as WSL): ~/.vscode-server/extensions

  • macOS: ~/.vscode/extensions

  • Windows: %USERPROFILE%\.vscode\extensions

As an exception, on NixOS, the extension makes a copy of the server and stores it under ~/.config/Code/User/globalStorage/rust-lang.rust-analyzer.

Note that we only support the 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 by testing the newest code, you can enable pre-release versions in the Code extension page.

Manual installation

Alternatively, download a VSIX corresponding to your platform 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

If you are running an unsupported platform, you can install rust-analyzer-no-server.vsix and compile or obtain a server binary. Copy the server anywhere, then add the path to your settings.json, for example:

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

Building From Source

Both the server and the Code plugin can be installed from source:

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

You’ll need Cargo, nodejs (matching a supported version of VS Code) 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

Make sure that .cargo/bin is in $PATH and precedes paths where rust-analyzer may also be installed. Specifically, rustup includes a proxy called rust-analyzer, which can cause problems if you’re planning to use a source build or even a downloaded binary.

rust-analyzer Language Server Binary

Other editors generally require the rust-analyzer binary to be in $PATH. You can download pre-built binaries from the releases page. You will need to uncompress and rename the binary for your platform, e.g. from rust-analyzer-aarch64-apple-darwin.gz on Mac OS to rust-analyzer, make it executable, then move it into a directory in your $PATH.

On Linux to install the rust-analyzer binary into ~/.local/bin, these commands should work:

1
2
3
$ mkdir -p ~/.local/bin
$ curl -L https://github.com/rust-lang/rust-analyzer/releases/latest/download/rust-analyzer-x86_64-unknown-linux-gnu.gz | gunzip -c - > ~/.local/bin/rust-analyzer
$ chmod +x ~/.local/bin/rust-analyzer

Make sure that ~/.local/bin is listed in the $PATH variable and use the appropriate URL if you’re not on a x86-64 system.

You don’t have to use ~/.local/bin, any other path like ~/.cargo/bin or /usr/local/bin will work just as well.

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-lang/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:

1
$ rustup component add rust-analyzer

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

Gentoo Linux

There are two ways to install rust-analyzer under Gentoo:

  • when installing dev-lang/rust or dev-lang/rust-bin, enable the rust-analyzer and rust-src USE flags

  • use the rust-analyzer component in rustup (see instructions above)

Note that in both cases, the version installed lags for a couple of months behind the official releases on GitHub. To obtain a newer one, you can download a binary from GitHub Releases or building from source.

macOS

The rust-analyzer binary can be installed via Homebrew.

1
$ brew install rust-analyzer

VS Code or VSCodium in Flatpak

Setting up rust-analyzer with a Flatpak version of Code is not trivial because of the Flatpak sandbox. While the sandbox can be disabled for some directories, /usr/bin will always be mounted under /run/host/usr/bin. This prevents access to the system’s C compiler, a system-wide installation of Rust, or any other libraries you might want to link to. Some compilers and libraries can be acquired as Flatpak SDKs, such as org.freedesktop.Sdk.Extension.rust-stable or org.freedesktop.Sdk.Extension.llvm15.

If you use a Flatpak SDK for Rust, there should be no extra steps necessary.

If you want to use Flatpak in combination with rustup, the following steps might help:

  • both Rust and rustup have to be installed using https://rustup.rs. Distro packages will not work.

  • you need to launch Code, open a terminal and run echo $PATH

  • using Flatseal, you must add an environment variable called PATH. Set its value to the output from above, appending :~/.cargo/bin, where ~ is the path to your home directory. You must replace ~, as it won’t be expanded otherwise.

  • while Flatseal is open, you must enable access to "All user files"

A C compiler should already be available via org.freedesktop.Sdk. Any other tools or libraries you will need to acquire from Flatpak.

Emacs

Prerequisites: You have installed the rust-analyzer binary.

To use rust-analyzer, you need to install and enable one of the two popular LSP client implementations for Emacs, Eglot or LSP Mode. Both enable rust-analyzer by default in rust buffers if it is available.

Eglot

Eglot is the more minimalistic and lightweight LSP client for Emacs, integrates well with existing Emacs functionality and is built into Emacs starting from release 29.

After installing Eglot, e.g. via M-x package-install (not needed from Emacs 29), you can enable it via the M-x eglot command or load it automatically in rust-mode via

1
(add-hook 'rust-mode-hook 'eglot-ensure)

To enable clippy, you will need to configure the initialization options to pass the check.command setting.

1
2
3
(add-to-list 'eglot-server-programs
             '((rust-ts-mode rust-mode) .
               ("rust-analyzer" :initializationOptions (:check (:command "clippy")))))

For more detailed instructions and options see the Eglot manual (also available from Emacs via M-x info) and the Eglot readme.

Eglot does not support the rust-analyzer extensions to the language-server protocol and does not aim to do so in the future. The eglot-x package adds experimental support for those LSP extensions.

LSP Mode

LSP-mode is the original LSP-client for emacs. Compared to Eglot it has a larger codebase and supports more features, like LSP protocol extensions. With extension packages like LSP UI it offers a lot of visual eyecandy. Further it integrates well with DAP mode for support of the Debug Adapter Protocol.

You can install LSP-mode via M-x package-install and then run it via the M-x lsp command or load it automatically in rust buffers with

1
(add-hook 'rust-mode-hook 'lsp-deferred)

For more information on how to set up LSP mode and its extension package see the instructions in the LSP mode manual. Also see the rust-analyzer section for rust-analyzer specific options and commands, which you can optionally bind to keys.

Note the excellent guide from @rksm on how to set-up Emacs for Rust development with LSP mode and several other packages.

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.

There 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

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 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
26
27
28
29
lua << EOF
local lspconfig = require'lspconfig'

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

lspconfig.rust_analyzer.setup({
    on_attach = on_attach,
    settings = {
        ["rust-analyzer"] = {
            imports = {
                granularity = {
                    group = "module",
                },
                prefix = "self",
            },
            cargo = {
                buildScripts = {
                    enable = true,
                },
            },
            procMacro = {
                enable = true
            },
        }
    }
})
EOF

If you’re running Neovim 0.10 or later, you can enable inlay hints via on_attach:

1
2
3
4
5
lspconfig.rust_analyzer.setup({
    on_attach = function(client, bufnr)
        vim.lsp.inlay_hint.enable(bufnr)
    end
})

Note that the hints are only visible after rust-analyzer has finished loading and you have to edit the file to trigger a re-render.

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

Check out https://github.com/mrcjkb/rustaceanvim for a batteries included rust-analyzer setup for Neovim.

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
16
17
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': {
        \       'buildScripts': {
        \         'enable': v:true,
        \       },
        \     },
        \     'procMacro': {
        \       'enable': v:true,
        \     },
        \   },
        \ })
endif

Sublime Text

Sublime Text 4:

Install LSP-file-watcher-chokidar to enable file watching (workspace/didChangeWatchedFiles).

Sublime Text 3:

If it worked, you should see "rust-analyzer, Line X, Column Y" on the left side of the status bar, and after waiting a bit, functionalities 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.

Kate Text Editor

Support for the language server protocol is built into Kate through the LSP plugin, which is included by default. It is preconfigured to use rust-analyzer for Rust sources since Kate 21.12.

To change rust-analyzer config options, start from the following example and put it into Kate’s "User Server Settings" tab (located under the LSP Client settings):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
    "servers": {
        "rust": {
            "initializationOptions": {
                "cachePriming": {
                    "enable": false
                },
                "check": {
                    "allTargets": false
                },
                "checkOnSave": false
            }
        }
    }
}

Then click on apply, and restart the LSP server for your rust project.

juCi++

juCi++ has built-in support for the language server protocol, and since version 1.7.0 offers installation of both Rust and rust-analyzer when opening a Rust file.

Kakoune

Kakoune supports LSP with the help of kak-lsp. Follow the instructions to install kak-lsp. To configure kak-lsp, refer to the configuration section which is basically about copying the configuration file in the right place (latest versions should use rust-analyzer by default).

Finally, you need to configure Kakoune to talk to kak-lsp (see Usage section). A basic configuration will only get you LSP but you can also activate inlay diagnostics and auto-formatting on save. The following might help you get all of this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
eval %sh{kak-lsp --kakoune -s $kak_session}  # Not needed if you load it with plug.kak.
hook global WinSetOption filetype=rust %{
    # Enable LSP
    lsp-enable-window

    # Auto-formatting on save
    hook window BufWritePre .* lsp-formatting-sync

    # Configure inlay hints (only on save)
    hook window -group rust-inlay-hints BufWritePost .* rust-analyzer-inlay-hints
    hook -once -always window WinSetOption filetype=.* %{
        remove-hooks window rust-inlay-hints
    }
}

Helix

Helix supports LSP by default. However, it won’t install rust-analyzer automatically. You can follow instructions for installing rust-analyzer binary.

Visual Studio 2022

There are multiple rust-analyzer extensions for Visual Studio 2022 on Windows:

rust-analyzer.vs

(License: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International)

Support for Rust development in the Visual Studio IDE is enabled by the rust-analyzer package. Either click on the download link or install from IDE’s extension manager. For now Visual Studio 2022 is required. All editions are supported viz. Community, Professional & Enterprise. The package aims to provide 0-friction installation and therefore comes loaded with most things required including rust-analyzer binary. If anything it needs is missing, appropriate errors / warnings will guide the user. E.g. cargo.exe needs to be in path and the package will tell you as much. This package is under rapid active development. So if you encounter any issues please file it at rust-analyzer.vs.

VS_RustAnalyzer

(License: GPL)

SourceGear Rust

(License: closed source)

  • Free (no-cost)

  • Supports all editions of Visual Studio 2022 on Windows: Community, Professional, or Enterprise

Lapce

Lapce has a Rust plugin which you can install directly. Unfortunately, it downloads an old version of rust-analyzer, but you can set the server path under Settings.

Crates

There is a package named ra_ap_rust_analyzer available on crates.io, for someone who wants to use it programmatically.

For more details, see the publish workflow.

Zed

Zed has native rust-analyzer support. If the LSP binary is not available, Zed can install it when opening a Rust file.

Troubleshooting

Start with looking at the rust-analyzer version. Try rust-analyzer: Show RA Version in VS Code (using Command Palette feature typically activated by Ctrl+Shift+P) or rust-analyzer --version in the command line. If the date is more than a week ago, it’s better to update rust-analyzer version.

The next thing to check would be panic messages in rust-analyzer’s log. Log messages are printed to stderr, in VS Code you can see them in the Output > Rust Analyzer Language Server tab of the panel. To see more logs, set the RA_LOG=info environment variable, this can be done either by setting the environment variable manually or by using rust-analyzer.server.extraEnv, note that both of these approaches require the server to be restarted.

To fully capture LSP messages between the editor and the server, set "rust-analyzer.trace.server": "verbose" config and check Output > Rust Analyzer Language Server Trace.

The root cause for many “nothing works” problems is that rust-analyzer fails to understand the project structure. To debug that, first note the rust-analyzer section in the status bar. If it has an error icon and red, that’s the problem (hover will have somewhat helpful error message). rust-analyzer: Status prints dependency information for the current file. Finally, RA_LOG=project_model=debug enables verbose logs during project loading.

If rust-analyzer outright crashes, try running rust-analyzer analysis-stats /path/to/project/directory/ on the command line. This command type checks the whole project in batch mode bypassing LSP machinery.

When filing issues, it is useful (but not necessary) to try to minimize examples. An ideal bug reproduction looks like this:

1
2
3
4
5
$ git clone https://github.com/username/repo.git && cd repo && git switch --detach commit-hash
$ rust-analyzer --version
rust-analyzer dd12184e4 2021-05-08 dev
$ rust-analyzer analysis-stats .
💀 💀 💀

It is especially useful when the repo doesn’t use external crates or the standard library.

If you want to go as far as to modify the source code to debug the problem, be sure to take a look at the dev docs!

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
 9
10
{
  "cargo": {
    "buildScripts": {
      "enable": 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.emitMustUse (default: false)

Whether to insert #[must_use] when generating as_ methods for enum variants.

rust-analyzer.assist.expressionFillDefault (default: "todo")

Placeholder expression to use for missing expressions in assists.

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

Warm up caches on project load.

rust-analyzer.cachePriming.numThreads (default: 0)

How many worker threads to handle priming caches. The default 0 means to pick automatically.

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

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

rust-analyzer.cargo.buildScripts.enable (default: true)

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

rust-analyzer.cargo.buildScripts.invocationLocation (default: "workspace")

Specifies the working directory for running build scripts. - "workspace": run build scripts for a workspace in the workspace’s root directory. This is incompatible with rust-analyzer.cargo.buildScripts.invocationStrategy set to once. - "root": run build scripts in the project’s root directory. This config only has an effect when rust-analyzer.cargo.buildScripts.overrideCommand is set.

rust-analyzer.cargo.buildScripts.invocationStrategy (default: "per_workspace")

Specifies the invocation strategy to use when running the build scripts command. If per_workspace is set, the command will be executed for each workspace. If once is set, the command will be executed once. This config only has an effect when rust-analyzer.cargo.buildScripts.overrideCommand is set.

rust-analyzer.cargo.buildScripts.overrideCommand (default: null)

Override the command rust-analyzer uses to run build scripts and build procedural macros. The command is required to output json and should therefore include --message-format=json or a similar option.

If there are multiple linked projects/workspaces, this command is invoked for each of them, with the working directory being the workspace root (i.e., the folder containing the Cargo.toml). This can be overwritten by changing rust-analyzer.cargo.buildScripts.invocationStrategy and rust-analyzer.cargo.buildScripts.invocationLocation.

By default, a cargo invocation will be constructed for the configured targets and features, with the following base command line:

1
cargo check --quiet --workspace --message-format=json --all-targets

.

rust-analyzer.cargo.buildScripts.rebuildOnSave (default: true)

Rerun proc-macros building/build-scripts running when proc-macro or build-script sources change and are saved.

rust-analyzer.cargo.buildScripts.useRustcWrapper (default: true)

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

rust-analyzer.cargo.cfgs (default: {})

List of cfg options to enable with the given values.

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

Extra arguments that are passed to every cargo invocation.

rust-analyzer.cargo.extraEnv (default: {})

Extra environment variables that will be set when running cargo, rustc or other commands within the workspace. Useful for setting RUSTFLAGS.

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

List of features to activate.

Set this to "all" to pass --all-features to cargo.

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

Whether to pass --no-default-features to cargo.

rust-analyzer.cargo.sysroot (default: "discover")

Relative path to the sysroot, or "discover" to try to automatically find it via "rustc --print sysroot".

Unsetting this disables sysroot loading.

This option does not take effect until rust-analyzer is restarted.

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

Whether to run cargo metadata on the sysroot library allowing rust-analyzer to analyze third-party dependencies of the standard libraries.

This will cause cargo to create a lockfile in your sysroot directory. rust-analyzer will attempt to clean up afterwards, but nevertheless requires the location to be writable to.

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

Relative path to the sysroot library sources. If left unset, this will default to {cargo.sysroot}/lib/rustlib/src/rust/library.

This option does not take effect until rust-analyzer is restarted.

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

Compilation target override (target triple).

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

Optional path to a rust-analyzer specific target directory. This prevents rust-analyzer’s cargo check and initial build-script and proc-macro building from locking the Cargo.lock at the expense of duplicating build artifacts.

Set to true to use a subdirectory of the existing target directory or set to a path relative to the workspace to use that path.

rust-analyzer.cargo.unsetTest (default: ["core"])

Unsets the implicit #[cfg(test)] for the specified crates.

rust-analyzer.checkOnSave (default: true)

Run the check command for diagnostics on save.

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

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

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

Cargo command to use for cargo check.

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

Extra arguments for cargo check.

rust-analyzer.check.extraEnv (default: {})

Extra environment variables that will be set when running cargo check. Extends rust-analyzer.cargo.extraEnv.

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

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

Set to "all" to pass --all-features to Cargo.

rust-analyzer.check.ignore (default: [])

List of cargo check (or other command specified in check.command) diagnostics to ignore.

For example for cargo check: dead_code, unused_imports, unused_variables,…​

rust-analyzer.check.invocationLocation (default: "workspace")

Specifies the working directory for running checks. - "workspace": run checks for workspaces in the corresponding workspaces' root directories. This falls back to "root" if rust-analyzer.check.invocationStrategy is set to once. - "root": run checks in the project’s root directory. This config only has an effect when rust-analyzer.check.overrideCommand is set.

rust-analyzer.check.invocationStrategy (default: "per_workspace")

Specifies the invocation strategy to use when running the check command. If per_workspace is set, the command will be executed for each workspace. If once is set, the command will be executed once. This config only has an effect when rust-analyzer.check.overrideCommand is set.

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

Whether to pass --no-default-features to Cargo. Defaults to rust-analyzer.cargo.noDefaultFeatures.

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

Override the command rust-analyzer uses instead of cargo check for diagnostics on save. The command is required to output json and should therefore include --message-format=json or a similar option (if your client supports the colorDiagnosticOutput experimental capability, you can use --message-format=json-diagnostic-rendered-ansi).

If you’re changing this because you’re using some tool wrapping Cargo, you might also want to change rust-analyzer.cargo.buildScripts.overrideCommand.

If there are multiple linked projects/workspaces, this command is invoked for each of them, with the working directory being the workspace root (i.e., the folder containing the Cargo.toml). This can be overwritten by changing rust-analyzer.check.invocationStrategy and rust-analyzer.check.invocationLocation.

If $saved_file is part of the command, rust-analyzer will pass the absolute path of the saved file to the provided command. This is intended to be used with non-Cargo build systems. Note that $saved_file is experimental and may be removed in the futureg.

An example command would be:

1
cargo check --workspace --message-format=json --all-targets

.

rust-analyzer.check.targets (default: null)

Check for specific targets. Defaults to rust-analyzer.cargo.target if empty.

Can be a single target, e.g. "x86_64-unknown-linux-gnu" or a list of targets, e.g. ["aarch64-apple-darwin", "x86_64-apple-darwin"].

Aliased as "checkOnSave.targets".

rust-analyzer.check.workspace (default: true)

Whether --workspace should be passed to cargo check. If false, -p <package> will be passed instead.

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.completion.autoself.enable (default: true)

Toggles the additional completions that automatically show method calls and field accesses with self prefixed to them when inside a method.

rust-analyzer.completion.callable.snippets (default: "fill_arguments")

Whether to add parenthesis and argument snippets when completing function.

rust-analyzer.completion.fullFunctionSignatures.enable (default: false)

Whether to show full function/method signatures in completion docs.

rust-analyzer.completion.limit (default: null)

Maximum number of completions to return. If None, the limit is infinite.

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

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

rust-analyzer.completion.privateEditable.enable (default: false)

Enables completions of private items and fields that are defined in the current workspace even if they are not visible at the current position.

rust-analyzer.completion.snippets.custom

Default:

{
            "Arc::new": {
                "postfix": "arc",
                "body": "Arc::new(${receiver})",
                "requires": "std::sync::Arc",
                "description": "Put the expression into an `Arc`",
                "scope": "expr"
            },
            "Rc::new": {
                "postfix": "rc",
                "body": "Rc::new(${receiver})",
                "requires": "std::rc::Rc",
                "description": "Put the expression into an `Rc`",
                "scope": "expr"
            },
            "Box::pin": {
                "postfix": "pinbox",
                "body": "Box::pin(${receiver})",
                "requires": "std::boxed::Box",
                "description": "Put the expression into a pinned `Box`",
                "scope": "expr"
            },
            "Ok": {
                "postfix": "ok",
                "body": "Ok(${receiver})",
                "description": "Wrap the expression in a `Result::Ok`",
                "scope": "expr"
            },
            "Err": {
                "postfix": "err",
                "body": "Err(${receiver})",
                "description": "Wrap the expression in a `Result::Err`",
                "scope": "expr"
            },
            "Some": {
                "postfix": "some",
                "body": "Some(${receiver})",
                "description": "Wrap the expression in an `Option::Some`",
                "scope": "expr"
            }
        }

Custom completion snippets.

rust-analyzer.completion.termSearch.enable (default: false)

Whether to enable term search based snippets like Some(foo.bar().baz()).

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

List of rust-analyzer diagnostics to disable.

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

Whether to show native rust-analyzer diagnostics.

rust-analyzer.diagnostics.experimental.enable (default: false)

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

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.styleLints.enable (default: false)

Whether to run additional style lints.

rust-analyzer.diagnostics.warningsAsHint (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.diagnostics.warningsAsInfo (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.files.excludeDirs (default: [])

These directories will be ignored by rust-analyzer. They are relative to the workspace root, and globs are not supported. You may also need to add the folders to Code’s files.watcherExclude.

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

Controls file watching implementation.

rust-analyzer.highlightRelated.breakPoints.enable (default: true)

Enables highlighting of related references while the cursor is on break, loop, while, or for keywords.

rust-analyzer.highlightRelated.closureCaptures.enable (default: true)

Enables highlighting of all captures of a closure while the cursor is on the | or move keyword of a closure.

rust-analyzer.highlightRelated.exitPoints.enable (default: true)

Enables highlighting of all exit points while the cursor is on any return, ?, fn, or return type arrow ().

rust-analyzer.highlightRelated.references.enable (default: true)

Enables highlighting of related references while the cursor is on any identifier.

rust-analyzer.highlightRelated.yieldPoints.enable (default: true)

Enables highlighting of all break points for a loop or block context while the cursor is on any async or await keywords.

rust-analyzer.hover.actions.debug.enable (default: true)

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

rust-analyzer.hover.actions.enable (default: true)

Whether to show HoverActions in Rust files.

rust-analyzer.hover.actions.gotoTypeDef.enable (default: true)

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

rust-analyzer.hover.actions.implementations.enable (default: true)

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

rust-analyzer.hover.actions.references.enable (default: false)

Whether to show References action. Only applies when rust-analyzer.hover.actions.enable is set.

rust-analyzer.hover.actions.run.enable (default: true)

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

rust-analyzer.hover.documentation.enable (default: true)

Whether to show documentation on hover.

rust-analyzer.hover.documentation.keywords.enable (default: true)

Whether to show keyword hover popups. Only applies when rust-analyzer.hover.documentation.enable is set.

rust-analyzer.hover.links.enable (default: true)

Use markdown syntax for links on hover.

rust-analyzer.hover.memoryLayout.alignment (default: "hexadecimal")

How to render the align information in a memory layout hover.

rust-analyzer.hover.memoryLayout.enable (default: true)

Whether to show memory layout data on hover.

rust-analyzer.hover.memoryLayout.niches (default: false)

How to render the niche information in a memory layout hover.

rust-analyzer.hover.memoryLayout.offset (default: "hexadecimal")

How to render the offset information in a memory layout hover.

rust-analyzer.hover.memoryLayout.size (default: "both")

How to render the size information in a memory layout hover.

rust-analyzer.hover.show.traitAssocItems (default: null)

How many associated items of a trait to display when hovering a trait.

rust-analyzer.imports.granularity.enforce (default: false)

Whether to enforce the import granularity setting for all files. If set to false rust-analyzer will try to keep import styles consistent per file.

rust-analyzer.imports.granularity.group (default: "crate")

How imports should be grouped into use statements.

rust-analyzer.imports.group.enable (default: true)

Group inserted imports by the following order. Groups are separated by newlines.

rust-analyzer.imports.merge.glob (default: true)

Whether to allow import insertion to merge new imports into single path glob imports like use std::fmt::*;.

rust-analyzer.imports.preferNoStd (default: false)

Prefer to unconditionally use imports of the core and alloc crate, over the std crate.

rust-analyzer.imports.preferPrelude (default: false)

Whether to prefer import paths containing a prelude module.

rust-analyzer.imports.prefix (default: "plain")

The path structure for newly inserted paths to use.

rust-analyzer.inlayHints.bindingModeHints.enable (default: false)

Whether to show inlay type hints for binding modes.

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

Whether to show inlay type hints for method chains.

rust-analyzer.inlayHints.closingBraceHints.enable (default: true)

Whether to show inlay hints after a closing } to indicate what item it belongs to.

rust-analyzer.inlayHints.closingBraceHints.minLines (default: 25)

Minimum number of lines required before the } until the hint is shown (set to 0 or 1 to always show them).

rust-analyzer.inlayHints.closureCaptureHints.enable (default: false)

Whether to show inlay hints for closure captures.

rust-analyzer.inlayHints.closureReturnTypeHints.enable (default: "never")

Whether to show inlay type hints for return types of closures.

rust-analyzer.inlayHints.closureStyle (default: "impl_fn")

Closure notation in type and chaining inlay hints.

rust-analyzer.inlayHints.discriminantHints.enable (default: "never")

Whether to show enum variant discriminant hints.

rust-analyzer.inlayHints.expressionAdjustmentHints.enable (default: "never")

Whether to show inlay hints for type adjustments.

rust-analyzer.inlayHints.expressionAdjustmentHints.hideOutsideUnsafe (default: false)

Whether to hide inlay hints for type adjustments outside of unsafe blocks.

rust-analyzer.inlayHints.expressionAdjustmentHints.mode (default: "prefix")

Whether to show inlay hints as postfix ops (. instead of , etc).

rust-analyzer.inlayHints.implicitDrops.enable (default: false)

Whether to show implicit drop hints.

rust-analyzer.inlayHints.lifetimeElisionHints.enable (default: "never")

Whether to show inlay type hints for elided lifetimes in function signatures.

rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames (default: false)

Whether to prefer using parameter names as the name for elided lifetime hints if possible.

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

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

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

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

rust-analyzer.inlayHints.rangeExclusiveHints.enable (default: false)

Whether to show exclusive range inlay hints.

rust-analyzer.inlayHints.reborrowHints.enable (default: "never")

Whether to show inlay hints for compiler inserted reborrows. This setting is deprecated in favor of rust-analyzer.inlayHints.expressionAdjustmentHints.enable.

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

Whether to render leading colons for type hints, and trailing colons for parameter hints.

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

Whether to show inlay type hints for variables.

rust-analyzer.inlayHints.typeHints.hideClosureInitialization (default: false)

Whether to hide inlay type hints for let statements that initialize to a closure. Only applies to closures with blocks, same as rust-analyzer.inlayHints.closureReturnTypeHints.enable.

rust-analyzer.inlayHints.typeHints.hideNamedConstructor (default: false)

Whether to hide inlay type hints for constructors.

rust-analyzer.interpret.tests (default: false)

Enables the experimental support for interpreting tests.

rust-analyzer.joinLines.joinAssignments (default: true)

Join lines merges consecutive declaration and initialization of an assignment.

rust-analyzer.joinLines.joinElseIf (default: true)

Join lines inserts else between consecutive ifs.

rust-analyzer.joinLines.removeTrailingComma (default: true)

Join lines removes trailing commas.

rust-analyzer.joinLines.unwrapTrivialBlock (default: true)

Join lines unwraps trivial blocks.

rust-analyzer.lens.debug.enable (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.forceCustomCommands (default: true)

Internal config: use custom client-side commands even when the client doesn’t set the corresponding capability.

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

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

rust-analyzer.lens.location (default: "above_name")

Where to render annotations.

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

Whether to show References lens for Struct, Enum, and Union. Only applies when rust-analyzer.lens.enable is set.

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

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

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

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

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

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

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

Whether to show Run 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.lru.capacity (default: null)

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

rust-analyzer.lru.query.capacities (default: {})

Sets the LRU capacity of the specified queries.

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

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

rust-analyzer.notifications.unindexedProject (default: false)

Whether to send an UnindexedProject notification to the client.

rust-analyzer.numThreads (default: null)

How many worker threads in the main loop. The default null means to pick automatically.

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

Expand attribute macros. Requires rust-analyzer.procMacro.enable to be set.

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

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

rust-analyzer.procMacro.ignored (default: {})

These proc-macros will be ignored when trying to expand them.

This config takes a map of crate names with the exported proc-macro names to ignore as values.

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

Internal config, path to proc-macro server executable.

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

Exclude imports from find-all-references.

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

Exclude tests from find-all-references.

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

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

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

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

rust-analyzer.rustc.source (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 if the rustc-dev component is installed.

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

This option does not take effect until rust-analyzer is restarted.

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. This should be the equivalent of rustfmt here, and not that of cargo fmt. The file contents will be passed on the standard input and the formatted result will be read from the standard output.

rust-analyzer.rustfmt.rangeFormatting.enable (default: false)

Enables the use of rustfmt’s unstable range formatting command for the textDocument/rangeFormatting request. The rustfmt option is unstable and only available on a nightly build.

rust-analyzer.semanticHighlighting.doc.comment.inject.enable (default: true)

Inject additional highlighting into doc comments.

When enabled, rust-analyzer will highlight rust source in doc comments as well as intra doc links.

rust-analyzer.semanticHighlighting.nonStandardTokens (default: true)

Whether the server is allowed to emit non-standard tokens and modifiers.

rust-analyzer.semanticHighlighting.operator.enable (default: true)

Use semantic tokens for operators.

When disabled, rust-analyzer will emit semantic tokens only for operator tokens when they are tagged with modifiers.

rust-analyzer.semanticHighlighting.operator.specialization.enable (default: false)

Use specialized semantic tokens for operators.

When enabled, rust-analyzer will emit special token types for operator tokens instead of the generic operator token type.

rust-analyzer.semanticHighlighting.punctuation.enable (default: false)

Use semantic tokens for punctuation.

When disabled, rust-analyzer will emit semantic tokens only for punctuation tokens when they are tagged with modifiers or have a special role.

rust-analyzer.semanticHighlighting.punctuation.separate.macro.bang (default: false)

When enabled, rust-analyzer will emit a punctuation semantic token for the ! of macro calls.

rust-analyzer.semanticHighlighting.punctuation.specialization.enable (default: false)

Use specialized semantic tokens for punctuation.

When enabled, rust-analyzer will emit special token types for punctuation tokens instead of the generic punctuation token type.

rust-analyzer.semanticHighlighting.strings.enable (default: true)

Use semantic tokens for strings.

In some editors (e.g. vscode) semantic tokens override other highlighting grammars. By disabling semantic tokens for strings, other grammars can be used to highlight their contents.

rust-analyzer.signatureInfo.detail (default: "full")

Show full signature of the callable. Only shows parameters if disabled.

rust-analyzer.signatureInfo.documentation.enable (default: true)

Show documentation.

rust-analyzer.typing.autoClosingAngleBrackets.enable (default: false)

Whether to insert closing angle brackets when typing an opening angle bracket of a generic argument list.

rust-analyzer.workspace.symbol.search.kind (default: "only_types")

Workspace symbol search kind.

rust-analyzer.workspace.symbol.search.limit (default: 128)

Limits the number of items returned from a workspace symbol search (Defaults to 128). Some clients like vs-code issue new searches on result filtering and don’t require all results to be returned in the initial search. Other clients requires all results upfront and might require a higher limit.

rust-analyzer.workspace.symbol.search.scope (default: "workspace")

Workspace symbol search scope.

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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
interface JsonProject {
    /// Path to the sysroot directory.
    ///
    /// The sysroot is where rustc looks for the
    /// crates that are built-in to rust, such as
    /// std.
    ///
    /// https://doc.rust-lang.org/rustc/command-line-arguments.html#--sysroot-override-the-system-root
    ///
    /// To see the current value of sysroot, you
    /// can query rustc:
    ///
    /// ```
    /// $ rustc --print sysroot
    /// /Users/yourname/.rustup/toolchains/stable-x86_64-apple-darwin
    /// ```
    sysroot?: string;
    /// Path to the directory with *source code* of
    /// sysroot crates.
    ///
    /// By default, this is `lib/rustlib/src/rust/library`
    /// relative to the sysroot.
    ///
    /// 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; },

    /// Whether the crate is a proc-macro crate.
    is_proc_macro: boolean;
    /// 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 three ways to feed rust-project.json to rust-analyzer:

  • Place rust-project.json file at the root of the project, and rust-analyzer 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 the RA_LOG environment variable to rust_analyzer=info to inspect how rust-analyzer handles config and project loading.

Note that calls to cargo check are disabled when using rust-project.json by default, so compilation errors and warnings will no longer be sent to your LSP client. To enable these compilation errors you will need to specify explicitly what command rust-analyzer should run to perform the checks using the rust-analyzer.check.overrideCommand configuration. As an example, the following configuration explicitly sets cargo check as the check command.

1
{ "rust-analyzer.check.overrideCommand": ["cargo", "check", "--message-format=json"] }

check.overrideCommand requires the command specified to output json error messages for rust-analyzer to consume. The --message-format=json flag does this for cargo check so whichever command you use must also output errors in this format. See the Configuration section for more information.

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

  • rust-toolchain.toml 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.

Privacy

The LSP server performs no network access in itself, but runs cargo metadata which will update or download the crate registry and the source code of the project dependencies. If enabled (the default), build scripts and procedural macros can do anything.

The Code extension does not access the network.

Any other editor plugins are not under the control of the rust-analyzer developers. For any privacy concerns, you should check with their respective developers.

For rust-analyzer developers, cargo xtask release uses the GitHub API to put together the release notes.

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;
Import Granularity

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

  • crate: Merge imports from the same crate into a single use statement. This kind of nesting is only supported in Rust versions later than 1.24.

  • module: Merge imports from the same module into a single use statement.

  • item: Don’t merge imports at all, creating one import per item.

  • preserve: Do not change the granularity of any imports. For auto-import this has the same effect as item.

  • one: Merge all imports into a single use statement as long as they have the same visibility and attributes.

In VS Code the configuration for this is rust-analyzer.imports.granularity.group.

Import Prefix

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

  • 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.

  • 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.imports.prefix.

113020673 b85be580 917a 11eb 9022 59585f35d4f8

Completion With Autoimport

Source: flyimport.rs

When completing names in the current scope, proposes additional imports from other modules or crates, if they can be qualified in the scope, and their name contains all symbols from the completion input.

To be considered applicable, the name must contain all input symbols in the given order, not necessarily adjacent. If any input symbol is not lowercased, the name must contain all symbols in exact case; otherwise the containing is checked case-insensitively.

1
2
3
4
fn main() {
    pda$0
}
# pub mod std { pub mod marker { pub struct PhantomData { } } }

1
2
3
4
5
6
use std::marker::PhantomData;

fn main() {
    PhantomData
}
# pub mod std { pub mod marker { pub struct PhantomData { } } }

Also completes associated items, that require trait imports. If any unresolved and/or partially-qualified path precedes the input, it will be taken into account. Currently, only the imports with their import path ending with the whole qualifier will be proposed (no fuzzy matching for qualifier).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
mod foo {
    pub mod bar {
        pub struct Item;

        impl Item {
            pub const TEST_ASSOC: usize = 3;
        }
    }
}

fn main() {
    bar::Item::TEST_A$0
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
use foo::bar;

mod foo {
    pub mod bar {
        pub struct Item;

        impl Item {
            pub const TEST_ASSOC: usize = 3;
        }
    }
}

fn main() {
    bar::Item::TEST_ASSOC
}
currently, if an assoc item comes from a trait that’s not currently imported, and it also has an unresolved and/or partially-qualified path, no imports will be proposed.
Fuzzy search details

To avoid an excessive amount of the results returned, completion input is checked for inclusion in the names only (i.e. in HashMap in the std::collections::HashMap path). For the same reasons, avoids searching for any path imports for inputs with their length less than 2 symbols (but shows all associated items for any input length).

Import configuration

It is possible to configure how use-trees are merged with the imports.granularity.group setting. Mimics the corresponding behavior of the Auto Import feature.

LSP and performance implications

The feature is enabled only if the LSP client supports LSP protocol version 3.16+ and reports the additionalTextEdits (case-sensitive) resolve client capability in its client capabilities. This way the server is able to defer the costly computations, doing them for a selected completion item only. For clients with no such support, all edits have to be calculated on the completion request, including the fuzzy search completion ones, which might be slow ergo the feature is automatically disabled.

Feature toggle

The feature can be forcefully turned off in the settings with the rust-analyzer.completion.autoimport.enable flag. Note that having this flag set to true does not guarantee that the feature is enabled: your client needs to have the corresponding capability enabled.

Debug ItemTree

Displays the ItemTree of the currently open file, for debugging.

Editor Action Name

VS Code

rust-analyzer: Debug ItemTree

Expand Macro Recursively

Source: expand_macro.rs

Shows the full macro expansion of the macro at the current caret position.

Editor Action Name

VS Code

rust-analyzer: Expand macro recursively at caret

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.

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

Folding

Defines folding regions for curly braced blocks, runs of consecutive use, mod, const or static items, and region / endregion comment markers.

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 Declaration

Navigates to the declaration of an identifier.

This is the same as Go to Definition with the following exceptions: - outline modules will navigate to the mod name; item declaration - trait assoc items will navigate to the assoc item of the trait declaration opposed to the trait impl - fields in patterns will navigate to the field declaration of the struct, union or variant

Go to Definition

Navigates to the definition of an identifier.

For outline modules, this will navigate to the source file of the module.

Editor Shortcut

VS Code

F12

113065563 025fbe00 91b1 11eb 83e4 a5a703610b23

Go to Implementation

Navigates to the impl blocks of types.

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

Highlights constructs related to the thing under the cursor:

  1. if on an identifier, highlights all references to that identifier in the current file

    1. additionally, if the identifier is a trait in a where clause, type parameter trait bound or use item, highlights all references to that trait’s assoc items in the corresponding scope

  2. if on an async or await token, highlights all yield points for that async context

  3. if on a return or fn keyword, ? character or return type arrow, highlights all exit points for that context

  4. if on a break, loop, while or for token, highlights all break points for that loop or block context

  5. if on a move or | token that belongs to a closure, highlights all captures of the closure.

Note: ?, | and do not currently trigger this behavior in the VSCode editor.

Hover

Source: hover.rs

Shows additional information, like the type of an expression or the documentation for a 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 by default shows hints for

  • types of local variables

  • names of function arguments

  • types of chained expressions

Optionally, one can enable additional hints for

  • return types of closure expressions

  • elided lifetimes

  • compiler inserted reborrows

113020660 b5f98b80 917a 11eb 8d70 3be3fd558cdd

Interpret Function

Editor Action Name

VS Code

rust-analyzer: Interpret Function

Join Lines

Source: join_lines.rs

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

See this gif for the cases handled specially by joined lines.

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.letelet $1 = expr else { $0 };

  • 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 against the completion input.

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.

Note that, depending on the other installed extensions, this feature can visibly slow down typing. Similarly, if rust-analyzer crashes or stops responding, Enter might not work. In that case, you can still press Shift-Enter to insert a newline.

VS Code

Add the following to keybindings.json:

1
2
3
4
5
{
  "key": "Enter",
  "command": "rust-analyzer.onEnter",
  "when": "editorTextFocus && !suggestWidgetVisible && editorLangId == rust"
}

When using the Vim plugin:

1
2
3
4
5
{
  "key": "Enter",
  "command": "rust-analyzer.onEnter",
  "when": "editorTextFocus && !suggestWidgetVisible && editorLangId == rust && vim.mode == 'Insert'"
}
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 = between two expressions adds ; when in statement position

  • typing = to turn an assignment into an equality comparison removes ; when in expression position

  • typing . in a chain method call auto-indents

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

  • typing { in a use item adds a closing } in the right place

    VS Code

    Add the following to settings.json:

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

Open Docs

Source: doc_links.rs

Retrieve a links to documentation for the given symbol.

The simplest way to use this feature is via the context menu. Right-click on the selected item. The context menu opens. Select Open Docs.

Editor Action Name

VS Code

rust-analyzer: Open Docs

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 a 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.

Token Tags

Rust-analyzer currently emits the following token tags:

  • For items:

    attribute

    Emitted for attribute macros.

    enum

    Emitted for enums.

    function

    Emitted for free-standing functions.

    derive

    Emitted for derive macros.

    macro

    Emitted for function-like macros.

    method

    Emitted for associated functions, also knowns as methods.

    namespace

    Emitted for modules.

    struct

    Emitted for structs.

    trait

    Emitted for traits.

    typeAlias

    Emitted for type aliases and Self in `impl`s.

    union

    Emitted for unions.

  • For literals:

    boolean

    Emitted for the boolean literals true and false.

    character

    Emitted for character literals.

    number

    Emitted for numeric literals.

    string

    Emitted for string literals.

    escapeSequence

    Emitted for escaped sequences inside strings like \n.

    formatSpecifier

    Emitted for format specifiers {:?} in format!-like macros.

  • For operators:

    operator

    Emitted for general operators.

    arithmetic

    Emitted for the arithmetic operators +, -, , /, +=, -=, =, /=.

    bitwise

    Emitted for the bitwise operators |, &, !, ^, |=, &=, ^=.

    comparison

    Emitted for the comparison operators >, <, ==, >=, , !=.

    logical

    Emitted for the logical operators ||, &&, !.

  • For punctuation:

    punctuation

    Emitted for general punctuation.

    attributeBracket

    Emitted for attribute invocation brackets, that is the #[ and ] tokens.

    angle

    Emitted for <> angle brackets.

    brace

    Emitted for {} braces.

    bracket

    Emitted for [] brackets.

    parenthesis

    Emitted for () parentheses.

    colon

    Emitted for the : token.

    comma

    Emitted for the , token.

    dot

    Emitted for the . token.

    semi

    Emitted for the ; token.

    macroBang

    Emitted for the ! token in macro calls.

builtinAttribute

Emitted for names to builtin attributes in attribute path, the repr in #[repr(u8)] for example.

builtinType

Emitted for builtin types like u32, str and f32.

comment

Emitted for comments.

constParameter

Emitted for const parameters.

deriveHelper

Emitted for derive helper attributes.

enumMember

Emitted for enum variants.

generic

Emitted for generic tokens that have no mapping.

keyword

Emitted for keywords.

label

Emitted for labels.

lifetime

Emitted for lifetimes.

parameter

Emitted for non-self function parameters.

property

Emitted for struct and union fields.

selfKeyword

Emitted for the self function parameter and self path-specifier.

selfTypeKeyword

Emitted for the Self type parameter.

toolModule

Emitted for tool modules.

typeParameter

Emitted for type parameters.

unresolvedReference

Emitted for unresolved references, names that rust-analyzer can’t find the definition of.

variable

Emitted for locals, constants and statics.

Token Modifiers

Token modifiers allow to style some elements in the source code more precisely.

Rust-analyzer currently emits the following token modifiers:

async

Emitted for async functions and the async and await keywords.

attribute

Emitted for tokens inside attributes.

callable

Emitted for locals whose types implements one of the Fn* traits.

constant

Emitted for consts.

consuming

Emitted for locals that are being consumed when use in a function call.

controlFlow

Emitted for control-flow related tokens, this includes the ? operator.

crateRoot

Emitted for crate names, like serde and crate.

declaration

Emitted for names of definitions, like foo in fn foo() {}.

defaultLibrary

Emitted for items from built-in crates (std, core, alloc, test and proc_macro).

documentation

Emitted for documentation comments.

injected

Emitted for doc-string injected highlighting like rust source blocks in documentation.

intraDocLink

Emitted for intra doc links in doc-strings.

library

Emitted for items that are defined outside of the current crate.

macro

Emitted for tokens inside macro calls.

mutable

Emitted for mutable locals and statics as well as functions taking &mut self.

public

Emitted for items that are from the current crate and are pub.

reference

Emitted for locals behind a reference and functions taking self by reference.

static

Emitted for "static" functions, also known as functions that do not take a self param, as well as statics and consts.

trait

Emitted for associated trait items.

unsafe

Emitted for unsafe operations, like unsafe function calls, as well as the unsafe token.

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

Show Dependency Tree

Source: fetch_crates.rs

Shows a view tree with all the dependencies of this project

Editor Panel Name

VS Code

Rust Dependencies

229394139 2625beab f4c9 484b 84ed ad5dee0b1e1a

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

Shuffle Crate Graph

Randomizes all crate IDs in the crate graph, for debugging.

Editor Action Name

VS Code

rust-analyzer: Shuffle Crate Graph

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)

User Snippet Completions

Source: snippet.rs

rust-analyzer allows the user to define custom (postfix)-snippets that may depend on items to be accessible for the current scope to be applicable.

A custom snippet can be defined by adding it to the rust-analyzer.completion.snippets.custom object respectively.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
  "rust-analyzer.completion.snippets.custom": {
    "thread spawn": {
      "prefix": ["spawn", "tspawn"],
      "body": [
        "thread::spawn(move || {",
        "\t$0",
        "});",
      ],
      "description": "Insert a thread::spawn call",
      "requires": "std::thread",
      "scope": "expr",
    }
  }
}

In the example above:

  • "thread spawn" is the name of the snippet.

  • prefix defines one or more trigger words that will trigger the snippets completion. Using postfix will instead create a postfix snippet.

  • body is one or more lines of content joined via newlines for the final output.

  • description is an optional description of the snippet, if unset the snippet name will be used.

  • requires is an optional list of item paths that have to be resolvable in the current crate where the completion is rendered. On failure of resolution the snippet won’t be applicable, otherwise the snippet will insert an import for the items on insertion if the items aren’t yet in scope.

  • scope is an optional filter for when the snippet should be applicable. Possible values are:

    • for Snippet-Scopes: expr, item (default: item)

    • for Postfix-Snippet-Scopes: expr, type (default: expr)

The body field also has access to placeholders as visible in the example as $0. These placeholders take the form of $number or ${number:placeholder_text} which can be traversed as tabstop in ascending order starting from 1, with $0 being a special case that always comes last.

There is also a special placeholder, ${receiver}, which will be replaced by the receiver expression for postfix snippets, or a $0 tabstop in case of normal snippets. This replacement for normal snippets allows you to reuse a snippet for both post- and prefix in a single definition.

For the VSCode editor, rust-analyzer also ships with a small set of defaults which can be removed by overwriting the settings object mentioned above, the defaults are:

 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
{
    "Arc::new": {
        "postfix": "arc",
        "body": "Arc::new(${receiver})",
        "requires": "std::sync::Arc",
        "description": "Put the expression into an `Arc`",
        "scope": "expr"
    },
    "Rc::new": {
        "postfix": "rc",
        "body": "Rc::new(${receiver})",
        "requires": "std::rc::Rc",
        "description": "Put the expression into an `Rc`",
        "scope": "expr"
    },
    "Box::pin": {
        "postfix": "pinbox",
        "body": "Box::pin(${receiver})",
        "requires": "std::boxed::Box",
        "description": "Put the expression into a pinned `Box`",
        "scope": "expr"
    },
    "Ok": {
        "postfix": "ok",
        "body": "Ok(${receiver})",
        "description": "Wrap the expression in a `Result::Ok`",
        "scope": "expr"
    },
    "Err": {
        "postfix": "err",
        "body": "Err(${receiver})",
        "description": "Wrap the expression in a `Result::Err`",
        "scope": "expr"
    },
    "Some": {
        "postfix": "some",
        "body": "Some(${receiver})",
        "description": "Wrap the expression in an `Option::Some`",
        "scope": "expr"
    }
}

View Crate Graph

Renders the currently loaded crate graph as an SVG graphic. Requires the dot tool, which is part of graphviz, to be installed.

Only workspace crates are included, no crates.io dependencies or sysroot crates.

Editor Action Name

VS Code

rust-analyzer: View Crate Graph

View Hir

Source: view_hir.rs

Editor Action Name

VS Code

rust-analyzer: View Hir

113065588 068bdb80 91b1 11eb 9a78 0b4ef1e972fb

View Memory Layout

Displays the recursive memory layout of a datatype.

Editor Action Name

VS Code

rust-analyzer: View Memory Layout

View Mir

Source: view_mir.rs

Editor Action Name

VS Code

rust-analyzer: View Mir

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.

Note that filtering does not currently work in VSCode due to the editor never sending the special symbols to the language server. Instead, you can configure the filtering via the rust-analyzer.workspace.symbol.search.scope and rust-analyzer.workspace.symbol.search.kind settings.

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_braces

Source: add_braces.rs

Adds braces to lambda and match arm expressions.

Before
1
2
3
4
5
6
fn foo(n: i32) -> i32 {
    match n {
        1 => n + 1,
        _ => 0
    }
}
After
1
2
3
4
5
6
7
8
fn foo(n: i32) -> i32 {
    match n {
        1 => {
            n + 1
        },
        _ => 0
    }
}

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
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_label_to_loop

Adds a label to a loop.

Before
1
2
3
4
5
6
fn main() {
    loop {
        break;
        continue;
    }
}
After
1
2
3
4
5
6
fn main() {
    'l: loop {
        break 'l;
        continue 'l;
    }
}

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_missing_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 } => todo!(),
        Action::Stop => todo!(),
    }
}

add_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() { 42i32 }
After
1
fn foo() -> i32 { 42i32 }

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) {}
}

apply_demorgan_iterator

Apply De Morgan’s law to Iterator::all and Iterator::any.

This transforms expressions of the form !iter.any(|x| predicate(x)) into iter.all(|x| !predicate(x)) and vice versa. This also works the other way for Iterator::all into Iterator::any.

Before
1
2
3
4
5
6
fn main() {
    let arr = [1, 2, 3];
    if !arr.into_iter().┃any(|num| num == 4) {
        println!("foo");
    }
}
After
1
2
3
4
5
6
fn main() {
    let arr = [1, 2, 3];
    if arr.into_iter().all(|num| num != 4) {
        println!("foo");
    }
}

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();
}

bind_unused_param

Binds unused function parameter to an underscore.

Before
1
fn some_function(x: i32) {}
After
1
2
3
fn some_function(x: i32) {
    let _ = x;
}

bool_to_enum

Source: bool_to_enum.rs

This converts boolean local variables, fields, constants, and statics into a new enum with two variants Bool::True and Bool::False, as well as replacing all assignments with the variants and replacing all usages with == Bool::True or == Bool::False.

Before
1
2
3
4
5
6
7
fn main() {
    let bool = true;

    if bool {
        println!("foo");
    }
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#[derive(PartialEq, Eq)]
enum Bool { True, False }

fn main() {
    let bool = Bool::True;

    if bool == Bool::True {
        println!("foo");
    }
}

change_visibility

Adds or changes existing visibility specifier.

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

convert_bool_then_to_if

Converts a bool::then method call to an equivalent if expression.

Before
1
2
3
fn main() {
    (0 == 0).then(|| val)
}
After
1
2
3
4
5
6
7
fn main() {
    if 0 == 0 {
        Some(val)
    } else {
        None
    }
}

convert_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;
    });
}

convert_if_to_bool_then

Converts an if expression into a corresponding bool::then call.

Before
1
2
3
4
5
6
7
fn main() {
    if cond {
        Some(val)
    } else {
        None
    }
}
After
1
2
3
fn main() {
    cond.then(|| val)
}

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 = iter::repeat((9, 2));
    iter.for_each(|(x, y)| {
        println!("x: {}, y: {}", x, y);
    });
}
After
1
2
3
4
5
6
fn main() {
    let iter = iter::repeat((9, 2));
    for (x, y) in iter {
        println!("x: {}, y: {}", x, y);
    }
}

convert_let_else_to_match

Converts let-else statement to let statement and match expression.

Before
1
2
3
fn main() {
    let Ok(mut x) = f() else { return };
}
After
1
2
3
4
5
6
fn main() {
    let mut x = match f() {
        Ok(x) => x,
        _ => return,
    };
}

convert_match_to_let_else

Converts let statement with match initializer to let-else statement.

Before
1
2
3
4
5
6
fn foo(opt: Option<()>) {
    let val = match opt {
        Some(it) => it,
        None => return,
    };
}
After
1
2
3
fn foo(opt: Option<()>) {
    let Some(val) = opt else { return };
}

convert_named_struct_to_tuple_struct

Converts struct with named fields to tuple struct, and analogously for enum variants with named fields.

Before
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
struct Point { x: f32, y: f32 }

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

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

    pub fn y(&self) -> f32 {
        self.y
    }
}
After
 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
    }
}

convert_nested_function_to_closure

Converts a function that is defined within the body of another function into a closure.

Before
1
2
3
4
5
6
7
fn main() {
    fn foo(label: &str, number: u64) {
        println!("{}: {}", label, number);
    }

    foo("Bar", 100);
}
After
1
2
3
4
5
6
7
fn main() {
    let foo = |label: &str, number: u64| {
        println!("{}: {}", label, number);
    };

    foo("Bar", 100);
}

convert_to_guarded_return

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_return_type_to_struct

This converts the return type of a function from a tuple type into a tuple struct and updates the body accordingly.

Before
1
2
3
4
5
6
7
fn bar() {
    let (a, b, c) = foo();
}

fn foo() -> (u32, u32, u32) {
    (1, 2, 3)
}
After
1
2
3
4
5
6
7
8
9
fn bar() {
    let FooResult(a, b, c) = foo();
}

struct FooResult(u32, u32, u32);

fn foo() -> FooResult {
    FooResult(1, 2, 3)
}

convert_tuple_struct_to_named_struct

Converts tuple struct to struct with named fields, and analogously for tuple enum variants.

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
    }
}

convert_two_arm_bool_match_to_matches_macro

Convert 2-arm match that evaluates to a boolean into the equivalent matches! invocation.

Before
1
2
3
4
5
6
fn main() {
    match scrutinee {
        Some(val) if val.cond() => true,
        _ => false,
    }
}
After
1
2
3
fn main() {
    matches!(scrutinee, Some(val) if val.cond())
}

convert_while_to_loop

Replace a while with a loop.

Before
1
2
3
4
5
fn main() {
    while cond {
        foo();
    }
}
After
1
2
3
4
5
6
7
8
fn main() {
    loop {
        if !cond {
            break;
        }
        foo();
    }
}

destructure_struct_binding

Destructures a struct binding in place.

Before
1
2
3
4
5
6
7
8
9
struct Foo {
    bar: i32,
    baz: i32,
}
fn main() {
    let foo = Foo { bar: 1, baz: 2 };
    let bar2 = foo.bar;
    let baz2 = &foo.baz;
}
After
1
2
3
4
5
6
7
8
9
struct Foo {
    bar: i32,
    baz: i32,
}
fn main() {
    let Foo { bar, baz } = Foo { bar: 1, baz: 2 };
    let bar2 = bar;
    let baz2 = &baz;
}

destructure_tuple_binding

Destructures a tuple binding in place.

Before
1
2
3
4
fn main() {
    let t = (1,2);
    let v = t.0;
}
After
1
2
3
4
fn main() {
    let (_0, _1) = (1,2);
    let v = _0;
}

desugar_doc_comment

Desugars doc-comments to the attribute form.

Before
1
2
/// Multi-line┃
/// comment
After
1
2
#[doc = r"Multi-line
comment"]

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::{Bar, Baz};

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

extract_expressions_from_format_string

Move an expression out of a format string.

Before
1
2
3
fn main() {
    print!("{var} {x + 1}┃");
}
After
1
2
3
fn main() {
    print!("{var} {}", x + 1);
}

extract_function

Extracts selected statements and comments into new function.

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

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

extract_module

Extracts a selected region as separate module. All the references, visibility and imports are resolved.

Before
1
2
3
4
5
6
7
fn foo(name: i32) -> i32 {
    name + 1
}

fn bar(name: i32) -> i32 {
    name + 2
}
After
1
2
3
4
5
6
7
8
9
mod modname {
    pub(crate) fn foo(name: i32) -> i32 {
        name + 1
    }
}

fn bar(name: i32) -> i32 {
    name + 2
}

extract_struct_from_enum_variant

Extracts a struct from enum variant.

Before
1
enum A { One(u32, u32) }
After
1
2
3
struct One(u32, 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_record_pattern_fields

Fills fields by replacing rest pattern in record patterns.

Before
1
2
3
4
5
struct Bar { y: Y, z: Z }

fn foo(bar: Bar) {
    let Bar { .. } = bar;
}
After
1
2
3
4
5
struct Bar { y: Y, z: Z }

fn foo(bar: Bar) {
    let Bar { y, z  } = bar;
}

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_constant

Generate a named constant.

Before
1
2
3
4
5
struct S { i: usize }
impl S { pub fn new(n: usize) {} }
fn main() {
    let v = S::new(CAPACITY);
}
After
1
2
3
4
5
6
struct S { i: usize }
impl S { pub fn new(n: usize) {} }
fn main() {
    const CAPACITY: usize = ;
    let v = S::new(CAPACITY);
}

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 new() -> 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_delegate_methods

Generate delegate methods.

Before
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
struct Age(u8);
impl Age {
    fn age(&self) -> u8 {
        self.0
    }
}

struct Person {
    age: Age,
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
struct Age(u8);
impl Age {
    fn age(&self) -> u8 {
        self.0
    }
}

struct Person {
    age: Age,
}

impl Person {
    fn age(&self) -> u8 {
        self.age.age()
    }
}

generate_delegate_trait

Generate delegate trait implementation for `StructField`s.

Before
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
trait SomeTrait {
    type T;
    fn fn_(arg: u32) -> u32;
    fn method_(&mut self) -> bool;
}
struct A;
impl SomeTrait for A {
    type T = u32;

    fn fn_(arg: u32) -> u32 {
        42
    }

    fn method_(&mut self) -> bool {
        false
    }
}
struct B {
    a: A,
}
After
 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
trait SomeTrait {
    type T;
    fn fn_(arg: u32) -> u32;
    fn method_(&mut self) -> bool;
}
struct A;
impl SomeTrait for A {
    type T = u32;

    fn fn_(arg: u32) -> u32 {
        42
    }

    fn method_(&mut self) -> bool {
        false
    }
}
struct B {
    a: A,
}

impl SomeTrait for B {
    type T = <A as SomeTrait>::T;

    fn fn_(arg: u32) -> u32 {
        <A as SomeTrait>::fn_(arg)
    }

    fn method_(&mut self) -> bool {
        <A as SomeTrait>::method_(&mut self.a)
    }
}

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 core::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_doc_example

Generates a rustdoc example when editing an item’s documentation.

Before
1
2
/// Adds two numbers.┃
pub fn add(a: i32, b: i32) -> i32 { a + b }
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
/// Adds two numbers.
///
/// # Examples
///
/// ```
/// use test::add;
///
/// assert_eq!(add(a, b), );
/// ```
pub fn add(a: i32, b: i32) -> i32 { a + b }

generate_documentation_template

Adds a documentation template above a function definition / declaration.

Before
1
2
3
4
5
6
pub struct S;
impl S {
    pub unsafe fn set_len(&mut self, len: usize) -> Result<(), std::io::Error> {
        /* ... */
    }
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
pub struct S;
impl S {
    /// Sets the length of this [`S`].
    ///
    /// # Errors
    ///
    /// This function will return an error if .
    ///
    /// # Safety
    ///
    /// .
    pub unsafe fn set_len(&mut self, len: usize) -> Result<(), std::io::Error> {
        /* ... */
    }
}

generate_enum_as_method

Generate an as_ method for this 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 this 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
13
14
15
enum Version {
 Undefined,
 Minor,
 Major,
}

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

generate_enum_try_into_method

Generate a try_into_ method for this 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_enum_variant

Adds a variant to an enum.

Before
1
2
3
4
5
6
7
enum Countries {
    Ghana,
}

fn main() {
    let country = Countries::Lesotho;
}
After
1
2
3
4
5
6
7
8
enum Countries {
    Ghana,
    Lesotho,
}

fn main() {
    let country = Countries::Lesotho;
}

generate_from_impl_for_enum

Adds a From impl for this 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 {
    name: String,
}
After
1
2
3
4
5
6
7
8
9
struct Person {
    name: String,
}

impl Person {
    fn name(&self) -> &str {
        &self.name
    }
}

generate_getter_mut

Generate a mut getter method.

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

impl Person {
    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
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
8
struct MyStruct { data: Vec<String> }

impl MyStruct {
    #[must_use]
    pub fn len(&self) -> usize {
        self.data.len()
    }
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
struct MyStruct { data: Vec<String> }

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

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

generate_mut_trait_impl

Adds a IndexMut impl from the Index trait.

Before
1
2
3
4
5
6
7
8
9
pub enum Axis { X = 0, Y = 1, Z = 2 }

impl<T> core::ops::Index<Axis> for [T; 3] {
    type Output = T;

    fn index(&self, index: Axis) -> &Self::Output {
        &self[index as usize]
    }
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
pub enum Axis { X = 0, Y = 1, Z = 2 }

impl<T> core::ops::IndexMut<Axis> for [T; 3] {
    fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
        &self[index as usize]
    }
}

impl<T> core::ops::Index<Axis> for [T; 3] {
    type Output = T;

    fn index(&self, index: Axis) -> &Self::Output {
        &self[index as usize]
    }
}

generate_new

Source: generate_new.rs

Adds a fn new for a type.

Before
1
2
3
struct Ctx<T: Clone> {
     data: T,
}
After
1
2
3
4
5
6
7
8
9
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 {
    name: String,
}
After
1
2
3
4
5
6
7
8
9
struct Person {
    name: String,
}

impl Person {
    fn set_name(&mut self, name: String) {
        self.name = name;
    }
}

generate_trait_from_impl

Generate trait for an already defined inherent impl and convert impl to a trait impl.

Before
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
struct Foo<const N: usize>([i32; N]);

macro_rules! const_maker {
    ($t:ty, $v:tt) => {
        const CONST: $t = $v;
    };
}

impl<const N: usize> Foo<N> {
    // Used as an associated constant.
    const CONST_ASSOC: usize = N * 4;

    fn create() -> Option<()> {
        Some(())
    }

    const_maker! {i32, 7}
}
After
 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
struct Foo<const N: usize>([i32; N]);

macro_rules! const_maker {
    ($t:ty, $v:tt) => {
        const CONST: $t = $v;
    };
}

trait ${0:NewTrait}<const N: usize> {
    // Used as an associated constant.
    const CONST_ASSOC: usize = N * 4;

    fn create() -> Option<()>;

    const_maker! {i32, 7}
}

impl<const N: usize> ${0:NewTrait}<N> for Foo<N> {
    // Used as an associated constant.
    const CONST_ASSOC: usize = N * 4;

    fn create() -> Option<()> {
        Some(())
    }

    const_maker! {i32, 7}
}

generate_trait_impl

Adds a new trait impl for a type.

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

impl<T: Clone> ${0:_} for Ctx<T> {}

inline_call

Source: inline_call.rs

Inlines a function or method body creating a let statement per parameter unless the parameter can be inlined. The parameter will be inlined either if it the supplied argument is a simple local or if the parameter is only accessed inside the function body once.

Before
1
2
3
fn foo(name: Option<&str>) {
    let name = name.unwrap();
}
After
1
2
3
4
5
6
fn foo(name: Option<&str>) {
    let name = match name {
            Some(val) => val,
            None => panic!("called `Option::unwrap()` on a `None` value"),
        };
}

inline_const_as_literal

Evaluate and inline const variable as literal.

Before
1
2
3
4
5
const STRING: &str = "Hello, World!";

fn something() -> &'static str {
    STRING
}
After
1
2
3
4
5
const STRING: &str = "Hello, World!";

fn something() -> &'static str {
    "Hello, World!"
}

inline_into_callers

Source: inline_call.rs

Inline a function or method body into all of its callers where possible, creating a let statement per parameter unless the parameter can be inlined. The parameter will be inlined either if it the supplied argument is a simple local or if the parameter is only accessed inside the function body once. If all calls can be inlined the function will be removed.

Before
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
fn print(_: &str) {}
fn foo(word: &str) {
    if !word.is_empty() {
        print(word);
    }
}
fn bar() {
    foo("안녕하세요");
    foo("여러분");
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
fn print(_: &str) {}

fn bar() {
    {
        let word = "안녕하세요";
        if !word.is_empty() {
            print(word);
        }
    };
    {
        let word = "여러분";
        if !word.is_empty() {
            print(word);
        }
    };
}

inline_local_variable

Inlines a local variable.

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

inline_macro

Source: inline_macro.rs

Takes a macro and inlines it one step.

Before
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
macro_rules! num {
    (+$($t:tt)+) => (1 + num!($($t )+));
    (-$($t:tt)+) => (-1 + num!($($t )+));
    (+) => (1);
    (-) => (-1);
}

fn main() {
    let number = num!(+ + + - + +);
    println!("{number}");
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
macro_rules! num {
    (+$($t:tt)+) => (1 + num!($($t )+));
    (-$($t:tt)+) => (-1 + num!($($t )+));
    (+) => (1);
    (-) => (-1);
}

fn main() {
    let number = 1+num!(+ + - + +);
    println!("{number}");
}

inline_type_alias

Replace a type alias with its concrete type.

Before
1
2
3
4
5
type A<T = u32> = Vec<T>;

fn main() {
    let a: A;
}
After
1
2
3
4
5
type A<T = u32> = Vec<T>;

fn main() {
    let a: Vec<u32>;
}

inline_type_alias_uses

Inline a type alias into all of its uses where possible.

Before
1
2
3
4
5
6
7
type A = i32;
fn id(x: A) -> A {
    x
};
fn foo() {
    let _: A = 3;
}
After
1
2
3
4
5
6
7
fn id(x: i32) -> i32 {
    x
};
fn foo() {
    let _: i32 = 3;
}

into_to_qualified_from

Convert an into method call to a fully qualified from call.

Before
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
//- minicore: from
struct B;
impl From<i32> for B {
    fn from(a: i32) -> Self {
       B
    }
}

fn main() -> () {
    let a = 3;
    let b: B = a.into();
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
struct B;
impl From<i32> for B {
    fn from(a: i32) -> Self {
       B
    }
}

fn main() -> () {
    let a = 3;
    let b: B = B::from(a);
}

introduce_named_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) {}

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

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 }
}

line_to_block

Converts comments between block and single-line form.

Before
1
2
   // Multi-line┃
   // comment
After
1
2
3
4
  /*
  Multi-line
  comment
  */

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 neighbor 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 the current match arm with the following if their bodies are identical.

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(),
    }
}

merge_nested_if

This transforms if expressions of the form if x { if y {A} } into if x && y {A} This assist can only be applied with the cursor on if.

Before
1
2
3
fn main() {
   if x == 3 { if y == 4 { 1 } }
}
After
1
2
3
fn main() {
   if x == 3 && y == 4 { 1 }
}

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_const_to_impl

Move a local constant item in a method to impl’s associated constant. All the references will be qualified with Self::.

Before
1
2
3
4
5
6
7
8
9
struct S;
impl S {
    fn foo() -> usize {
        /// The answer.
        const C: usize = 42;

        C * C
    }
}
After
1
2
3
4
5
6
7
8
9
struct S;
impl S {
    /// The answer.
    const C: usize = 42;

    fn foo() -> usize {
        Self::C * Self::C
    }
}

move_from_mod_rs

Moves xxx/mod.rs to xxx.rs.

Before
1
2
3
4
//- /main.rs
mod a;
//- /a/mod.rs
fn t() {}
After
1
fn t() {}

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;

move_to_mod_rs

Moves xxx.rs to xxx/mod.rs.

Before
1
2
3
4
//- /main.rs
mod a;
//- /a.rs
fn t() {}
After
1
fn t() {}

normalize_import

Normalizes an import.

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

promote_local_to_const

Promotes a local variable to a const item changing its name to a SCREAMING_SNAKE_CASE variant if the local uses no non-const expressions.

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

    if foo {
        println!("It's true");
    } else {
        println!("It's false");
    }
}
After
1
2
3
4
5
6
7
8
9
fn main() {
    const FOO: bool = true;

    if FOO {
        println!("It's true");
    } else {
        println!("It's false");
    }
}

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_method_call

Replaces the method call with a qualified function call.

Before
1
2
3
4
5
6
7
8
struct Foo;
impl Foo {
    fn foo(&self) {}
}
fn main() {
    let foo = Foo;
    foo.foo();
}
After
1
2
3
4
5
6
7
8
struct Foo;
impl Foo {
    fn foo(&self) {}
}
fn main() {
    let foo = Foo;
    Foo::foo(&foo);
}

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();
}

reformat_number_literal

Adds or removes separators from integer literal.

Before
1
const _: i32 = 1012345;
After
1
const _: i32 = 1_012_345;

remove_dbg

Source: remove_dbg.rs

Removes dbg!() macro call.

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

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_parentheses

Removes redundant parentheses.

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

remove_unused_imports

Removes any use statements in the current selection that are unused.

Before
1
2
3
4
struct X();
mod foo {
    use super::X;
}
After
1
2
3
struct X();
mod foo {
}

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_items

Reorder the items of an impl Trait. The items 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 {
    type A;
    const B: u8;
    fn c();
}

struct Bar;
impl Foo for Bar {
    const B: u8 = 17;
    fn c() {}
    type A = String;
}
After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
trait Foo {
    type A;
    const B: u8;
    fn c();
}

struct Bar;
impl Foo for Bar {
    type A = String;
    const B: u8 = 17;
    fn c() {}
}

replace_arith_with_checked

Replaces arithmetic on integers with the checked_* equivalent.

Before
1
2
3
fn main() {
  let x = 1 + 2;
}
After
1
2
3
fn main() {
  let x = 1.checked_add(2);
}

replace_arith_with_saturating

Replaces arithmetic on integers with the saturating_* equivalent.

Before
1
2
3
fn main() {
  let x = 1 + 2;
}
After
1
2
3
fn main() {
  let x = 1.saturating_add(2);
}

replace_arith_with_wrapping

Replaces arithmetic on integers with the wrapping_* equivalent.

Before
1
2
3
fn main() {
  let x = 1 + 2;
}
After
1
2
3
fn main() {
  let x = 1.wrapping_add(2);
}

replace_char_with_string

Replace a char literal with a string literal.

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

replace_derive_with_manual_impl

Converts a derive impl into a manual one.

Before
1
2
#[derive(Debug, 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<()> {
        f.debug_struct("S").finish()
    }
}

replace_if_let_with_match

Replaces a if let expression 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_is_some_with_if_let_some

Replace if x.is_some() with if let Some(_tmp) = x or if x.is_ok() with if let Ok(_tmp) = x.

Before
1
2
3
4
fn main() {
    let x = Some(1);
    if x.is_some() {}
}
After
1
2
3
4
fn main() {
    let x = Some(1);
    if let Some(${0:x}) = x {}
}

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_named_generic_with_impl

Replaces named generic with an impl Trait in function argument.

Before
1
fn new<P: AsRef<Path>>(location: P) -> Self {}
After
1
fn new(location: impl AsRef<Path>) -> Self {}

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 literal with char literal.

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

replace_try_expr_with_match

Replaces a try expression with a match expression.

Before
1
2
3
fn handle() {
    let pat = Some(true)?;
}
After
1
2
3
4
5
6
fn handle() {
    let pat = match Some(true) {
        Some(it) => it,
        None => return None,
    };
}

replace_turbofish_with_explicit_type

Converts ::<_> to an explicit type assignment.

Before
1
2
3
4
fn make<T>() -> T { ) }
fn main() {
    let a = make::<i32>();
}
After
1
2
3
4
fn make<T>() -> T { ) }
fn main() {
    let a: i32 = make();
}

replace_with_eager_method

Replace unwrap_or_else with unwrap_or and ok_or_else with ok_or.

Before
1
2
3
4
fn foo() {
    let a = Some(1);
    a.unwrap_or_else(|| 2);
}
After
1
2
3
4
fn foo() {
    let a = Some(1);
    a.unwrap_or(2);
}

replace_with_lazy_method

Replace unwrap_or with unwrap_or_else and ok_or with ok_or_else.

Before
1
2
3
4
fn foo() {
    let a = Some(1);
    a.unwrap_or(2);
}
After
1
2
3
4
fn foo() {
    let a = Some(1);
    a.unwrap_or_else(|| 2);
}

sort_items

Source: sort_items.rs

Sorts item members alphabetically: fields, enum variants and methods.

Before
1
struct Foo { second: u32, first: String }
After
1
struct Foo { first: String, second: u32 }

Before
1
2
3
4
trait Bar {
    fn second(&self) -> u32;
    fn first(&self) -> String;
}
After
1
2
3
4
trait Bar {
    fn first(&self) -> String;
    fn second(&self) -> u32;
}

Before
1
2
3
4
5
struct Baz;
impl Baz {
    fn second(&self) -> u32;
    fn first(&self) -> String;
}
After
1
2
3
4
5
struct Baz;
impl Baz {
    fn first(&self) -> String;
    fn second(&self) -> u32;
}

There is a difference between sorting enum variants:

Before
1
2
3
4
enum Animal {
  Dog(String, f64),
  Cat { weight: f64, name: String },
}
After
1
2
3
4
enum Animal {
  Cat { weight: f64, name: String },
  Dog(String, f64),
}

and sorting a single enum struct variant:

Before
1
2
3
4
enum Animal {
  Dog(String, f64),
  Cat { weight: f64, name: String },
}
After
1
2
3
4
enum Animal {
  Dog(String, f64),
  Cat { name: String, weight: f64 },
}

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_match_arm

Splits the current match with a | pattern into two arms with identical bodies.

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

fn handle(action: Action) {
    match action {
        Action::Move(..) | Action::Stop => foo(),
    }
}
After
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(),
    }
}

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;

unnecessary_async

Removes the async mark from functions which have no .await in their body. Looks for calls to the functions and removes the .await on the call site.

Before
1
2
pub async fn foo() {}
pub async fn bar() { foo().await }
After
1
2
pub fn foo() {}
pub async fn bar() { foo() }

unqualify_method_call

Transforms universal function call syntax into a method call.

Before
1
2
3
fn main() {
    std::ops::Add::add(1, 2);
}
After
1
2
3
4
5
use std::ops::Add;

fn main() {
    1.add(2);
}

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");
}

unwrap_result_return_type

Unwrap the function’s return type.

Before
1
fn foo() -> Result<i32> { Ok(42i32) }
After
1
fn foo() -> i32 { 42i32 }

unwrap_tuple

Source: unwrap_tuple.rs

Unwrap the tuple to different variables.

Before
1
2
3
fn main() {
    let (foo, bar) = ("Foo", "Bar");
}
After
1
2
3
4
fn main() {
    let foo = "Foo";
    let bar = "Bar";
}

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.experimental.enable or rust-analyzer.diagnostics.disabled settings.

Clippy

To run cargo clippy instead of cargo check, you can set "rust-analyzer.check.command": "clippy".

break-outside-of-loop

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

expected-function

This diagnostic is triggered if a call is made on something that is not callable.

inactive-code

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

incoherent-impl

This diagnostic is triggered if the targe type of an impl is from a foreign crate.

incorrect-ident-case

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

invalid-derive-target

This diagnostic is shown when the derive attribute is used on an item other than a struct, enum or union.

macro-error

Source: macro_error.rs

This diagnostic is shown for macro expansion errors.

macro-error

Source: macro_error.rs

This diagnostic is shown for macro expansion errors.

malformed-derive

This diagnostic is shown when the derive attribute has invalid input.

mismatched-arg-count

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

mismatched-tuple-struct-pat-arg-count

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

missing-fields

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-match-arm

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

missing-unsafe

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

moved-out-of-ref

This diagnostic is triggered on moving non copy things out of references.

need-mut

This diagnostic is triggered on mutating an immutable variable.

no-such-field

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

non-exhaustive-let

This diagnostic is triggered if a let statement without an else branch has a non-exhaustive pattern.

private-assoc-item

This diagnostic is triggered if the referenced associated item is not visible from the current module.

private-field

This diagnostic is triggered if the accessed field is not visible from the current module.

remove-trailing-return

This diagnostic is triggered when there is a redundant return at the end of a function or closure.

remove-unnecessary-else

This diagnostic is triggered when there is an else block for an if expression whose then branch diverges (e.g. ends with a return, continue, break e.t.c).

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

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

trait-impl-incorrect-safety

Diagnoses incorrect safety annotations of trait impls.

trait-impl-missing-assoc_item

Diagnoses missing trait items in a trait impl.

trait-impl-orphan

Only traits defined in the current crate can be implemented for arbitrary types

trait-impl-redundant-assoc_item

Diagnoses redundant trait items in a trait impl.

type-mismatch

This diagnostic is triggered when the type of an expression or pattern does not match the expected type.

typed-hole

Source: typed_hole.rs

This diagnostic is triggered when an underscore expression is used in an invalid position.

undeclared-label

unimplemented-builtin-macro

This diagnostic is shown for builtin macros which are not yet implemented by rust-analyzer

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.

unnecessary-braces

Diagnostic for unnecessary braces in use items.

unreachable-label

unresolved-assoc-item

This diagnostic is triggered if the referenced associated item does not exist.

unresolved-extern-crate

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

unresolved-field

This diagnostic is triggered if a field does not exist on a given type.

unresolved-ident

This diagnostic is triggered if an expr-position ident is invalid.

unresolved-import

This diagnostic is triggered if rust-analyzer is unable to resolve a path in a use declaration.

unresolved-macro-call

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

unresolved-method

This diagnostic is triggered if a method does not exist on a given type.

unresolved-module

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

unresolved-proc-macro

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.attributes.enable).

unused-mut

This diagnostic is triggered when a mutable variable isn’t actually mutated.

unused-variables

This diagnostic is triggered when a local variable is not used.

Editor Features

VS Code

Color configurations

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
  "editor.inlayHints.fontFamily": "Courier New",
  "editor.inlayHints.fontSize": 11,

  "workbench.colorCustomizations": {
    // Name of the theme you are currently using
    "[Default Dark+]": {
      "editorInlayHint.foreground": "#868686f0",
      "editorInlayHint.background": "#3d3d3d48",

      // Overrides for specific kinds of inlay hints
      "editorInlayHint.typeForeground": "#fdb6fdf0",
      "editorInlayHint.parameterForeground": "#fdb6fdf0",
    }
  }
}

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
      },
    }
  },
}

Most themes doesn’t support styling unsafe operations differently yet. You can fix this by adding overrides for the rules operator.unsafe, function.unsafe, and method.unsafe:

1
2
3
4
5
6
7
8
9
{
   "editor.semanticTokenColorCustomizations": {
         "rules": {
             "operator.unsafe": "#ff6600",
             "function.unsafe": "#ff6600",
             "method.unsafe": "#ff6600"
         }
    },
}

In addition to the top-level rules you can specify overrides for specific themes. For example, if you wanted to use a darker text color on a specific light theme, you might write:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
   "editor.semanticTokenColorCustomizations": {
         "rules": {
             "operator.unsafe": "#ff6600"
         },
         "[Ayu Light]": {
            "rules": {
               "operator.unsafe": "#572300"
            }
         }
    },
}

Make sure you include the brackets around the theme name. For example, use "[Ayu Light]" to customize the theme Ayu Light.

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+alt+d",
  "command": "rust-analyzer.openDocs",
  "when": "inRustProject"
}

More about when clause contexts here.

Setting runnable environment variables

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

1
2
3
"rust-analyzer.runnables.extraEnv": {
    "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.runnables.extraEnv": [
    {
        // "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.

If needed, you can set different values for different platforms:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
"rust-analyzer.runnables.extraEnv": [
    {
        "platform": "win32", // windows only
        "env": {
             "APP_DATA": "windows specific data"
        }
    },
    {
        "platform": ["linux"],
        "env": {
             "APP_DATA": "linux data",
        }
    },
    { // for all platforms
        "env": {
             "APP_COMMON_DATA": "xxx",
        }
    }
]

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": 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
}

Live Share

VS Code Live Share has partial support for rust-analyzer.

Live Share requires the official Microsoft build of VS Code, OSS builds will not work correctly.

The host’s rust-analyzer instance will be shared with all guests joining the session. The guests do not have to have the rust-analyzer extension installed for this to work.

If you are joining a Live Share session and do have rust-analyzer installed locally, commands from the command palette will not work correctly since they will attempt to communicate with the local server.