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: The manual is written in AsciiDoc and includes some extra files which are generated from the source code. Run |
If you have questions about using rust-analyzer, please ask them in the “IDEs and Editors” topic of Rust users forum.
- Installation
- Troubleshooting
- Configuration
- Non-Cargo Based Projects
- Security
- Privacy
- Features
- Annotations
- Auto Import
- Completion With Autoimport
- Debug ItemTree
- Expand Macro Recursively
- Expand and Shrink Selection
- File Structure
- Find All References
- Folding
- Format String Completion
- Go to Declaration
- Go to Definition
- Go to Implementation
- Go to Type Definition
- Highlight Related
- Hover
- Inlay Hints
- Interpret A Function, Static Or Const.
- Join Lines
- Magic Completions
- Matching Brace
- Memory Usage
- Move Item
- On Enter
- On Typing Assists
- Open Docs
- Parent Module
- Related Tests
- Rename
- Run
- Semantic Syntax Highlighting
- Show Dependency Tree
- Show Syntax Tree
- Status
- Structural Search and Replace
- User Snippet Completions
- View Crate Graph
- View Hir
- View Memory Layout
- View Mir
- Workspace Symbol
- Assists (Code Actions)
- Diagnostics
- Clippy
- await-outside-of-async
- break-outside-of-loop
- cast-to-unsized
- expected-function
- generic-args-prohibited
- inactive-code
- incoherent-impl
- incorrect-ident-case
- invalid-cast
- invalid-derive-target
- macro-def-error
- macro-error
- malformed-derive
- mismatched-arg-count
- mismatched-tuple-struct-pat-arg-count
- missing-fields
- missing-match-arm
- missing-unsafe
- moved-out-of-ref
- need-mut
- no-such-field
- non-exhaustive-let
- private-assoc-item
- private-field
- proc-macro-disabled
- proc-macros-disabled
- remove-trailing-return
- remove-unnecessary-else
- replace-filter-map-next-with-find-map
- trait-impl-incorrect-safety
- trait-impl-missing-assoc_item
- trait-impl-orphan
- trait-impl-redundant-assoc_item
- type-mismatch
- typed-hole
- undeclared-label
- unimplemented-builtin-macro
- unlinked-file
- unnecessary-braces
- unreachable-label
- unresolved-assoc-item
- unresolved-extern-crate
- unresolved-field
- unresolved-ident
- unresolved-import
- unresolved-macro-call
- unresolved-method
- unresolved-module
- unused-mut
- unused-variables
- Editor Features
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.
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.
Arch Linux
The rust-analyzer
binary can be installed from the repos or AUR (Arch User Repository):
-
rust-analyzer
(built from latest tagged source) -
rust-analyzer-git
(latest Git version)
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
ordev-lang/rust-bin
, enable therust-analyzer
andrust-src
USE flags -
use the
rust-analyzer
component inrustup
(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.
Windows
It is recommended to install the latest Microsoft Visual C++ Redistributable prior to installation. Download links can be found here.
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, it must be in your PATH
:
-
install the SDK extensions with
flatpak install org.freedesktop.Sdk.Extension.{llvm15,rust-stable}//23.08
-
enable SDK extensions in the editor with the environment variable
FLATPAK_ENABLE_SDK_EXT=llvm15,rust-stable
(this can be done using flatseal orflatpak override
)
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.
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
-
Install coc.nvim by following the instructions at coc.nvim (Node.js required)
-
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
-
Install LanguageClient-neovim by following the instructions here
-
The GitHub project wiki has extra tips on configuration
-
-
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.
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(true, { bufnr = 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:
-
Follow the instructions in LSP-rust-analyzer.
Install LSP-file-watcher-chokidar to enable file watching (workspace/didChangeWatchedFiles ).
|
Sublime Text 3:
-
Install the
rust-analyzer
binary. -
Install the LSP package.
-
From the command palette, run
LSP: Enable Language Server Globally
and selectrust-analyzer
.
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.
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, run the rust-analyzer: Toggle LSP Logs
command 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.assist.termSearch.borrowcheck (default:
true
) -
Enable borrow checking for term search code assists. If set to false, also there will be more suggestions, but some of them may not borrow-check.
- rust-analyzer.assist.termSearch.fuel (default:
1800
) -
Term search fuel in "units of work" for assists (Defaults to 1800).
- rust-analyzer.cachePriming.enable (default:
true
) -
Warm up caches on project load.
- rust-analyzer.cachePriming.numThreads (default:
"physical"
) -
How many worker threads to handle priming caches. The default
0
means to pick automatically. - rust-analyzer.cargo.allTargets (default:
true
) -
Pass
--all-targets
to cargo invocation. - rust-analyzer.cargo.autoreload (default:
true
) -
Automatically refresh project info via
cargo metadata
onCargo.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.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 Rust workspace with the workspace as the working directory. Ifonce
is set, the command will be executed once with the opened project as the working directory. This config only has an effect whenrust-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 changingrust-analyzer.cargo.buildScripts.invocationStrategy
.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 --keep-going
.
- 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:
{ "miri": null, "debug_assertions": null }
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:
"cargo_metadata"
) -
How to query metadata for the sysroot crate. Using cargo metadata allows rust-analyzer to analyze third-party dependencies of the standard libraries.
- 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 theCargo.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.cfg.setTest (default:
true
) -
Set
cfg(test)
for local crates. Defaults to true. - rust-analyzer.checkOnSave (default:
true
) -
Run the check command for diagnostics on save.
- rust-analyzer.check.allTargets (default:
null
) -
Check all targets and tests (
--all-targets
). Defaults torust-analyzer.cargo.allTargets
. - 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
. Extendsrust-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 incheck.command
) diagnostics to ignore.For example for
cargo check
:dead_code
,unused_imports
,unused_variables
,… - 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. Ifonce
is set, the command will be executed once. This config only has an effect whenrust-analyzer.check.overrideCommand
is set. - rust-analyzer.check.noDefaultFeatures (default:
null
) -
Whether to pass
--no-default-features
to Cargo. Defaults torust-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 thecolorDiagnosticOutput
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 changingrust-analyzer.check.invocationStrategy
.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 future.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 tocargo check
. If false,-p <package>
will be passed instead. - rust-analyzer.completion.addSemicolonToUnit (default:
true
) -
Whether to automatically add a semicolon when completing unit-returning functions.
In
match
arms it completes a comma 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.hideDeprecated (default:
false
) -
Whether to omit deprecated items from autocompletion. By default they are marked as deprecated but not hidden.
- 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:
{ "Ok": { "postfix": "ok", "body": "Ok(${receiver})", "description": "Wrap the expression in a `Result::Ok`", "scope": "expr" }, "Box::pin": { "postfix": "pinbox", "body": "Box::pin(${receiver})", "requires": "std::boxed::Box", "description": "Put the expression into a pinned `Box`", "scope": "expr" }, "Arc::new": { "postfix": "arc", "body": "Arc::new(${receiver})", "requires": "std::sync::Arc", "description": "Put the expression into an `Arc`", "scope": "expr" }, "Some": { "postfix": "some", "body": "Some(${receiver})", "description": "Wrap the expression in an `Option::Some`", "scope": "expr" }, "Err": { "postfix": "err", "body": "Err(${receiver})", "description": "Wrap the expression in a `Result::Err`", "scope": "expr" }, "Rc::new": { "postfix": "rc", "body": "Rc::new(${receiver})", "requires": "std::rc::Rc", "description": "Put the expression into an `Rc`", "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.completion.termSearch.fuel (default:
1000
) -
Term search fuel in "units of work" for autocompletion (Defaults to 1000).
- 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
, orfor
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
orawait
keywords. - rust-analyzer.hover.actions.debug.enable (default:
true
) -
Whether to show
Debug
action. Only applies whenrust-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 whenrust-analyzer.hover.actions.enable
is set. - rust-analyzer.hover.actions.implementations.enable (default:
true
) -
Whether to show
Implementations
action. Only applies whenrust-analyzer.hover.actions.enable
is set. - rust-analyzer.hover.actions.references.enable (default:
false
) -
Whether to show
References
action. Only applies whenrust-analyzer.hover.actions.enable
is set. - rust-analyzer.hover.actions.run.enable (default:
true
) -
Whether to show
Run
action. Only applies whenrust-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.enumVariants (default:
5
) -
How many variants of an enum to display when hovering on. Show none if empty.
- rust-analyzer.hover.show.fields (default:
5
) -
How many fields of a struct, variant or union to display when hovering on. Show none if empty.
- 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.imports.prefixExternPrelude (default:
false
) -
Whether to prefix external (including std, core) crate imports with
::
. e.g. "use ::std::io::Read;". - 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.genericParameterHints.const.enable (default:
true
) -
Whether to show const generic parameter name inlay hints.
- rust-analyzer.inlayHints.genericParameterHints.lifetime.enable (default:
false
) -
Whether to show generic lifetime parameter name inlay hints.
- rust-analyzer.inlayHints.genericParameterHints.type.enable (default:
false
) -
Whether to show generic type parameter name inlay hints.
- 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 asrust-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 whenrust-analyzer.lens.enable
is set. - rust-analyzer.lens.enable (default:
true
) -
Whether to show CodeLens in Rust files.
- rust-analyzer.lens.implementations.enable (default:
true
) -
Whether to show
Implementations
lens. Only applies whenrust-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 whenrust-analyzer.lens.enable
is set. - rust-analyzer.lens.references.enumVariant.enable (default:
false
) -
Whether to show
References
lens for Enum Variants. Only applies whenrust-analyzer.lens.enable
is set. - rust-analyzer.lens.references.method.enable (default:
false
) -
Whether to show
Method References
lens. Only applies whenrust-analyzer.lens.enable
is set. - rust-analyzer.lens.references.trait.enable (default:
false
) -
Whether to show
References
lens for Trait. Only applies whenrust-analyzer.lens.enable
is set. - rust-analyzer.lens.run.enable (default:
true
) -
Whether to show
Run
lens. Only applies whenrust-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
,.rs
files (which will be treated as standalone files) or JSON objects inrust-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.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 and call-hierarchy.
- 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.runnables.extraTestBinaryArgs
-
Default:
[ "--show-output" ]
Additional arguments to be passed through Cargo to launched tests, benchmarks, or doc-tests.
Unless the launched target uses a [custom test harness](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-harness-field), they will end up being interpreted as options to [
rustc
’s built-in test harness (“libtest”)](https://doc.rust-lang.org/rustc/tests/index.html#cli-arguments). - 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 ofcargo 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.excludeChars (default:
"|<"
) -
Specify the characters to exclude from triggering typing assists. The default trigger characters are
.
,=
,<
,>
,{
, and(
. - rust-analyzer.workspace.discoverConfig (default:
null
) -
Enables automatic discovery of projects using [
DiscoverWorkspaceConfig::command
].[
DiscoverWorkspaceConfig
] also requires settingprogress_label
andfiles_to_watch
.progress_label
is used for the title in progress indicators, whereasfiles_to_watch
is used to determine which build system-specific files should be watched in order to reload rust-analyzer.Below is an example of a valid configuration:
1 2 3 4 5 6 7 8 9 10
"rust-analyzer.workspace.discoverConfig": { "command": [ "rust-project", "develop-json" ], "progressLabel": "rust-analyzer", "filesToWatch": [ "BUCK" ] }
## On
DiscoverWorkspaceConfig::command
Warning: This format is provisional and subject to change.
[
DiscoverWorkspaceConfig::command
] must return a JSON object corresponding toDiscoverProjectData::Finished
:1 2 3 4 5 6 7 8
#[derive(Debug, Clone, Deserialize, Serialize)] #[serde(tag = "kind")] #[serde(rename_all = "snake_case")] enum DiscoverProjectData { Finished { buildfile: Utf8PathBuf, project: ProjectJsonData }, Error { error: String, source: Option<String> }, Progress { message: String }, }
As JSON,
DiscoverProjectData::Finished
is:1 2 3 4 5 6 7 8 9 10 11 12
{ // the internally-tagged representation of the enum. "kind": "finished", // the file used by a non-Cargo build system to define // a package or target. "buildfile": "rust-analyzer/BUILD", // the contents of a rust-project.json, elided for brevity "project": { "sysroot": "foo", "crates": [] } }
It is encouraged, but not required, to use the other variants on
DiscoverProjectData
to provide a more polished end-user experience.DiscoverWorkspaceConfig::command
may optionally include an{arg}
, which will be substituted with the JSON-serialized form of the following enum:1 2 3 4 5 6
#[derive(PartialEq, Clone, Debug, Serialize)] #[serde(rename_all = "camelCase")] pub enum DiscoverArgument { Path(AbsPathBuf), Buildfile(AbsPathBuf), }
The JSON representation of
DiscoverArgument::Path
is:1 2 3
{ "path": "src/main.rs" }
Similarly, the JSON representation of
DiscoverArgument::Buildfile
is:1 2 3
{ "buildfile": "BUILD" }
DiscoverArgument::Path
is used to find and generate arust-project.json
, and therefore, a workspace, whereasDiscoverArgument::buildfile
is used to to update an existing workspace. As a reference for implementors, buck2’srust-project
will likely be useful: https://github.com/facebook/buck2/tree/main/integrations/rust-project. - 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
111
112
113
114
115
116
117
118
119
120
121
122
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;
/// List of groups of common cfg values, to allow
/// sharing them between crates.
///
/// Maps from group name to its cfgs. Cfg follow
/// the same format as `Crate.cfg`.
cfg_groups?: { [key: string]: 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[],
},
/// List of cfg groups this crate inherits.
///
/// All cfg in these groups will be concatenated to
/// `cfg`. It is impossible to replace a value from
/// the groups.
cfg_groups?: 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 overriderustc
with an arbitrary executable -
rust-toolchain.toml
can overriderustc
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
orrust-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.
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
andcore
-
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;
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 asitem
. -
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
.
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 thecrate
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 withself
. This will result in paths that always start with eithercrate
,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
.
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. |
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).
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.
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.
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
Source: view_item_tree.rs
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 |
Expand and Shrink Selection
Source: extend_selection.rs
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+← |
File Structure
Source: file_structure.rs
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 |
Find All References
Source: references.rs
Shows all references of the item at the cursor location
Editor | Shortcut |
---|---|
VS Code |
Shift+Alt+F12 |
Folding
Source: folding_ranges.rs
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:
-
format
→format!(…)
-
panic
→panic!(…)
-
println
→println!(…)
-
log
:-
logd
→log::debug!(…)
-
logt
→log::trace!(…)
-
logi
→log::info!(…)
-
logw
→log::warn!(…)
-
loge
→log::error!(…)
-
Go to Declaration
Source: goto_declaration.rs
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 as opposed to the trait impl
- fields in patterns will navigate to the field declaration of the struct, union or variant
Go to Definition
Source: goto_definition.rs
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 |
Go to Implementation
Source: goto_implementation.rs
Navigates to the impl items of types.
Editor | Shortcut |
---|---|
VS Code |
Ctrl+F12 |
Go to Type Definition
Source: goto_type_definition.rs
Navigates to the type of an identifier.
Editor | Action Name |
---|---|
VS Code |
Go to Type Definition |
Highlight Related
Source: highlight_related.rs
Highlights constructs related to the thing under the cursor:
-
if on an identifier, highlights all references to that identifier in the current file
-
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
-
-
if on an
async
orawait
token, highlights all yield points for that async context -
if on a
return
orfn
keyword,?
character or→
return type arrow, highlights all exit points for that context -
if on a
break
,loop
,while
orfor
token, highlights all break points for that loop or block context -
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.
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
-
names of const generic parameters
-
types of chained expressions
Optionally, one can enable additional hints for
-
return types of closure expressions
-
elided lifetimes
-
compiler inserted reborrows
-
names of generic type and lifetime parameters
Note: inlay hints for function argument names are heuristically omitted to reduce noise and will not appear if any of the following criteria are met:
-
the parameter name is a suffix of the function’s name
-
the argument is a qualified constructing or call expression where the qualifier is an ADT
-
exact argument<→parameter match(ignoring leading underscore) or parameter is a prefix/suffix of argument with _ splitting it off
-
the parameter name starts with
ra_fixture
-
the parameter name is a well known name in a unary function
-
the parameter name is a single character in a unary function
Interpret A Function, Static Or Const.
Source: interpret.rs
Editor | Action Name |
---|---|
VS Code |
rust-analyzer: Interpret |
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 |
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.if
→if expr {}
orif let … {}
forOption
orResult
-
expr.match
→match expr {}
-
expr.while
→while expr {}
orwhile let … {}
forOption
orResult
-
expr.ref
→&expr
-
expr.refm
→&mut expr
-
expr.let
→let $0 = expr;
-
expr.lete
→let $1 = expr else { $0 };
-
expr.letm
→let mut $0 = expr;
-
expr.not
→!expr
-
expr.dbg
→dbg!(expr)
-
expr.dbgr
→dbg!(&expr)
-
expr.call
→(expr)
There also snippet completions:
-
pd
→eprintln!(" = {:?}", );
-
ppd
→eprintln!(" = {:#?}", );
-
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.
Matching Brace
Source: matching_brace.rs
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 |
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) |
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 |
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'"
}
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 -
typing
>
to complete a return type→
will insert a whitespace after it- VS Code
-
Add the following to
settings.json
:
"editor.formatOnType": true,
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
Source: parent_module.rs
Navigates to the parent module of the current module.
Editor | Action Name |
---|---|
VS Code |
rust-analyzer: Locate parent module |
Related Tests
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 |
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 |
Semantic Syntax Highlighting
Source: syntax_highlighting.rs
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.
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
andfalse
.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
{:?}
informat!
-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 |
builtinType |
Emitted for builtin types like |
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 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 |
attribute |
Emitted for tokens inside attributes. |
callable |
Emitted for locals whose types implements one of the |
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 |
crateRoot |
Emitted for crate names, like |
declaration |
Emitted for names of definitions, like |
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 |
public |
Emitted for items that are from the current crate and are |
reference |
Emitted for locals behind a reference and functions taking |
static |
Emitted for "static" functions, also known as functions that do not take a |
trait |
Emitted for associated trait items. |
unsafe |
Emitted for unsafe operations, like unsafe function calls, as well as the |
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 |
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 |
Status
Source: status.rs
Shows internal statistic about memory usage of rust-analyzer.
Editor | Action Name |
---|---|
VS Code |
rust-analyzer: Status |
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. |
not(a) |
Negates the constraint |
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. Usingpostfix
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
Source: view_crate_graph.rs
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 Memory Layout
Source: view_memory_layout.rs
Displays the recursive memory layout of a datatype.
Editor | Action Name |
---|---|
VS Code |
rust-analyzer: View Memory Layout |
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 forFoo
type in the current workspace -
foo#
searches forfoo
function in the current workspace -
Foo*
searches forFoo
type among dependencies, includingstdlib
-
foo#*
searches forfoo
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. Symbols prefixed
with __
are hidden from the search results unless configured otherwise.
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.
1
2
3
4
5
6
fn foo(n: i32) -> i32 {
match n {
1 =>┃ n + 1,
_ => 0
}
}
1
2
3
4
5
6
7
8
fn foo(n: i32) -> i32 {
match n {
1 => {
n + 1
},
_ => 0
}
}
add_explicit_type
Source: add_explicit_type.rs
Specify type for a let binding.
1
2
3
fn main() {
let x┃ = 92;
}
1
2
3
fn main() {
let x: i32 = 92;
}
add_hash
Source: raw_string.rs
Adds a hash to a raw string literal.
1
2
3
fn main() {
r#"Hello,┃ World!"#;
}
1
2
3
fn main() {
r##"Hello, World!"##;
}
add_impl_default_members
Source: add_missing_impl_members.rs
Adds scaffold for overriding default impl members.
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) {}┃
}
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
Source: add_missing_impl_members.rs
Adds scaffold for required impl members.
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 () {┃
}
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
Source: add_label_to_loop.rs
Adds a label to a loop.
1
2
3
4
5
6
fn main() {
loop┃ {
break;
continue;
}
}
1
2
3
4
5
6
fn main() {
'l: loop {
break 'l;
continue 'l;
}
}
add_lifetime_to_type
Source: add_lifetime_to_type.rs
Adds a new lifetime to a struct, enum or union.
1
2
3
4
struct Point {
x: &┃u32,
y: u32,
}
1
2
3
4
struct Point<'a> {
x: &'a u32,
y: u32,
}
add_missing_match_arms
Source: add_missing_match_arms.rs
Adds missing clauses to a match
expression.
1
2
3
4
5
6
7
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
┃
}
}
1
2
3
4
5
6
7
8
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move { distance } => ${1:todo!()},
Action::Stop => ${2:todo!()},┃
}
}
add_return_type
Source: add_return_type.rs
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.
1
fn foo() { 4┃2i32 }
1
fn foo() -> i32 { 42i32 }
add_turbo_fish
Source: add_turbo_fish.rs
Adds ::<_>
to a call of a generic method or function.
1
2
3
4
fn make<T>() -> T { todo!() }
fn main() {
let x = make┃();
}
1
2
3
4
fn make<T>() -> T { todo!() }
fn main() {
let x = make::<${0:_}>();
}
apply_demorgan
Source: apply_demorgan.rs
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 &&
.
1
2
3
fn main() {
if x != 4 ||┃ y < 3.14 {}
}
1
2
3
fn main() {
if !(x == 4 && y >= 3.14) {}
}
apply_demorgan_iterator
Source: apply_demorgan.rs
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
.
1
2
3
4
5
6
fn main() {
let arr = [1, 2, 3];
if !arr.into_iter().┃any(|num| num == 4) {
println!("foo");
}
}
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.
1
2
3
fn main() {
let map = HashMap┃::new();
}
1
2
3
4
5
use std::collections::HashMap;
fn main() {
let map = HashMap::new();
}
bind_unused_param
Source: bind_unused_param.rs
Binds unused function parameter to an underscore.
1
fn some_function(x: i32┃) {}
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
.
1
2
3
4
5
6
7
fn main() {
let ┃bool = true;
if bool {
println!("foo");
}
}
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
Source: change_visibility.rs
Adds or changes existing visibility specifier.
1
┃fn frobnicate() {}
1
pub(crate) fn frobnicate() {}
comment_to_doc
Converts comments to documentation.
1
2
// Wow what ┃a nice module
// I sure hope this shows up when I hover over it
1
2
//! Wow what a nice module
//! I sure hope this shows up when I hover over it
convert_bool_then_to_if
Source: convert_bool_then.rs
Converts a bool::then
method call to an equivalent if expression.
1
2
3
fn main() {
(0 == 0).then┃(|| val)
}
1
2
3
4
5
6
7
fn main() {
if 0 == 0 {
Some(val)
} else {
None
}
}
convert_closure_to_fn
Source: convert_closure_to_fn.rs
This converts a closure to a freestanding function, changing all captures to parameters.
1
2
3
4
5
fn main() {
let mut s = String::new();
let closure = |┃a| s.push_str(a);
closure("abc");
}
1
2
3
4
5
6
7
fn main() {
let mut s = String::new();
fn closure(a: &str, s: &mut String) {
s.push_str(a)
}
closure("abc", &mut s);
}
convert_for_loop_with_for_each
Source: convert_iter_for_each_to_for.rs
Converts a for loop into a for_each loop on the Iterator.
1
2
3
4
5
6
fn main() {
let x = vec![1, 2, 3];
for┃ v in x {
let y = v * 2;
}
}
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_from_to_tryfrom
Source: convert_from_to_tryfrom.rs
Converts a From impl to a TryFrom impl, wrapping returns in Ok
.
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
}
}
}
1
2
3
4
5
6
7
8
9
10
impl TryFrom<usize> for Thing {
type Error = ${0:()};
fn try_from(val: usize) -> Result<Self, Self::Error> {
Ok(Thing {
b: val.to_string(),
a: val
})
}
}
convert_if_to_bool_then
Source: convert_bool_then.rs
Converts an if expression into a corresponding bool::then
call.
1
2
3
4
5
6
7
fn main() {
if┃ cond {
Some(val)
} else {
None
}
}
1
2
3
fn main() {
cond.then(|| val)
}
convert_integer_literal
Source: convert_integer_literal.rs
Converts the base of integer literals to other bases.
1
const _: i32 = 10┃;
1
const _: i32 = 0b1010;
convert_into_to_from
Source: convert_into_to_from.rs
Converts an Into impl to an equivalent From impl.
1
2
3
4
5
6
7
8
impl ┃Into<Thing> for usize {
fn into(self) -> Thing {
Thing {
b: self.to_string(),
a: self
}
}
}
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
Source: convert_iter_for_each_to_for.rs
Converts an Iterator::for_each function into a for loop.
1
2
3
4
5
6
fn main() {
let iter = iter::repeat((9, 2));
iter.for_each┃(|(x, y)| {
println!("x: {}, y: {}", x, y);
});
}
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
Source: convert_let_else_to_match.rs
Converts let-else statement to let statement and match expression.
1
2
3
fn main() {
let Ok(mut x) = f() else┃ { return };
}
1
2
3
4
5
6
fn main() {
let mut x = match f() {
Ok(x) => x,
_ => return,
};
}
convert_match_to_let_else
Source: convert_match_to_let_else.rs
Converts let statement with match initializer to let-else statement.
1
2
3
4
5
6
fn foo(opt: Option<()>) {
let val┃ = match opt {
Some(it) => it,
None => return,
};
}
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.
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
}
}
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.
1
2
3
4
5
6
7
fn main() {
fn fo┃o(label: &str, number: u64) {
println!("{}: {}", label, number);
}
foo("Bar", 100);
}
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
Source: convert_to_guarded_return.rs
Replace a large conditional with a guarded return.
1
2
3
4
5
6
fn main() {
┃if cond {
foo();
bar();
}
}
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.
1
2
3
4
5
6
7
fn bar() {
let (a, b, c) = foo();
}
fn foo() -> (┃u32, u32, u32) {
(1, 2, 3)
}
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.
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
}
}
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.
1
2
3
4
5
6
fn main() {
match scrutinee┃ {
Some(val) if val.cond() => true,
_ => false,
}
}
1
2
3
fn main() {
matches!(scrutinee, Some(val) if val.cond())
}
convert_while_to_loop
Source: convert_while_to_loop.rs
Replace a while with a loop.
1
2
3
4
5
fn main() {
┃while cond {
foo();
}
}
1
2
3
4
5
6
7
8
fn main() {
loop {
if !cond {
break;
}
foo();
}
}
destructure_struct_binding
Source: destructure_struct_binding.rs
Destructures a struct binding in place.
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;
}
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
Source: destructure_tuple_binding.rs
Destructures a tuple binding in place.
1
2
3
4
fn main() {
let ┃t = (1,2);
let v = t.0;
}
1
2
3
4
fn main() {
let (┃_0, _1) = (1,2);
let v = _0;
}
desugar_async_into_impl_future
Source: toggle_async_sugar.rs
Rewrites asynchronous function from async fn
into → impl Future
.
This action does not touch the function body and therefore 0
block does not transform to async { 0 }
.
1
2
3
pub as┃ync fn foo() -> usize {
0
}
1
2
3
pub fn foo() -> impl core::future::Future<Output = usize> {
0
}
desugar_doc_comment
Source: desugar_doc_comment.rs
Desugars doc-comments to the attribute form.
1
2
/// Multi-line┃
/// comment
1
2
#[doc = r"Multi-line
comment"]
expand_glob_import
Source: expand_glob_import.rs
Expands glob imports.
1
2
3
4
5
6
7
8
mod foo {
pub struct Bar;
pub struct Baz;
}
use foo::*┃;
fn qux(bar: Bar, baz: Baz) {}
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) {}
explicit_enum_discriminant
Source: explicit_enum_discriminant.rs
Adds explicit discriminant to all enum variants.
1
2
3
4
5
6
enum TheEnum┃ {
Foo,
Bar,
Baz = 42,
Quux,
}
1
2
3
4
5
6
enum TheEnum {
Foo = 0,
Bar = 1,
Baz = 42,
Quux = 43,
}
extract_constant
Source: extract_variable.rs
Extracts subexpression into a constant.
1
2
3
fn main() {
┃(1 + 2)┃ * 4;
}
1
2
3
4
fn main() {
const ┃VAR_NAME: i32 = 1 + 2;
VAR_NAME * 4;
}
extract_expressions_from_format_string
Move an expression out of a format string.
1
2
3
fn main() {
print!("{var} {x + 1}┃");
}
1
2
3
fn main() {
print!("{var} {}"┃, x + 1);
}
extract_function
Source: extract_function.rs
Extracts selected statements and comments into new function.
1
2
3
4
5
6
7
fn main() {
let n = 1;
┃let m = n + 2;
// calculate
let k = m + n;┃
let g = 3;
}
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
Source: extract_module.rs
Extracts a selected region as separate module. All the references, visibility and imports are resolved.
1
2
3
4
5
6
7
┃fn foo(name: i32) -> i32 {
name + 1
}┃
fn bar(name: i32) -> i32 {
name + 2
}
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_static
Source: extract_variable.rs
Extracts subexpression into a static.
1
2
3
fn main() {
┃(1 + 2)┃ * 4;
}
1
2
3
4
fn main() {
static ┃VAR_NAME: i32 = 1 + 2;
VAR_NAME * 4;
}
extract_struct_from_enum_variant
Extracts a struct from enum variant.
1
enum A { ┃One(u32, u32) }
1
2
3
struct One(u32, u32);
enum A { One(One) }
extract_type_alias
Source: extract_type_alias.rs
Extracts the selected type as a type alias.
1
2
3
struct S {
field: ┃(u8, u8, u8)┃,
}
1
2
3
4
5
type ┃Type = (u8, u8, u8);
struct S {
field: Type,
}
extract_variable
Source: extract_variable.rs
Extracts subexpression into a variable.
1
2
3
fn main() {
┃(1 + 2)┃ * 4;
}
1
2
3
4
fn main() {
let ┃var_name = 1 + 2;
var_name * 4;
}
fill_record_pattern_fields
Source: fill_record_pattern_fields.rs
Fills fields by replacing rest pattern in record patterns.
1
2
3
4
5
struct Bar { y: Y, z: Z }
fn foo(bar: Bar) {
let Bar { ..┃ } = bar;
}
1
2
3
4
5
struct Bar { y: Y, z: Z }
fn foo(bar: Bar) {
let Bar { y, z } = bar;
}
fix_visibility
Source: fix_visibility.rs
Makes inaccessible item public.
1
2
3
4
5
6
mod m {
fn frobnicate() {}
}
fn main() {
m::frobnicate┃();
}
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.
1
2
3
fn main() {
let _ = 90 +┃ 2;
}
1
2
3
fn main() {
let _ = 2 + 90;
}
flip_comma
Source: flip_comma.rs
Flips two comma-separated items.
1
2
3
fn main() {
((1, 2),┃ (3, 4));
}
1
2
3
fn main() {
((3, 4), (1, 2));
}
flip_trait_bound
Source: flip_trait_bound.rs
Flips two trait bounds.
1
fn foo<T: Clone +┃ Copy>() { }
1
fn foo<T: Copy + Clone>() { }
generate_constant
Source: generate_constant.rs
Generate a named constant.
1
2
3
4
5
struct S { i: usize }
impl S { pub fn new(n: usize) {} }
fn main() {
let v = S::new(CAPA┃CITY);
}
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.
1
2
3
4
5
enum Version {
Undefined,
Minor┃,
Major,
}
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
Source: generate_default_from_new.rs
Generates default implementation from new method.
1
2
3
4
5
6
7
struct Example { _inner: () }
impl Example {
pub fn n┃ew() -> Self {
Self { _inner: () }
}
}
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
Source: generate_delegate_methods.rs
Generate delegate methods.
1
2
3
4
5
6
7
8
9
10
struct Age(u8);
impl Age {
fn age(&self) -> u8 {
self.0
}
}
struct Person {
ag┃e: Age,
}
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
Source: generate_delegate_trait.rs
Generate delegate trait implementation for `StructField`s.
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,
}
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
Source: generate_deref.rs
Generate Deref
impl using the given struct field.
1
2
3
4
struct A;
struct B {
┃a: A
}
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
Source: generate_derive.rs
Adds a new #[derive()]
clause to a struct or enum.
1
2
3
4
struct Point {
x: u32,
y: u32,┃
}
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.
1
2
/// Adds two numbers.┃
pub fn add(a: i32, b: i32) -> i32 { a + b }
1
2
3
4
5
6
7
8
9
10
/// Adds two numbers.
///
/// # Examples
///
/// ```
/// use ra_test_fixture::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.
1
2
3
4
5
6
pub struct S;
impl S {
pub unsafe fn set_len┃(&mut self, len: usize) -> Result<(), std::io::Error> {
/* ... */
}
}
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.
1
2
3
4
enum Value {
Number(i32),
Text(String)┃,
}
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
Source: generate_enum_is_method.rs
Generate an is_
method for this enum variant.
1
2
3
4
5
enum Version {
Undefined,
Minor┃,
Major,
}
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.
1
2
3
4
enum Value {
Number(i32),
Text(String)┃,
}
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
Source: generate_enum_variant.rs
Adds a variant to an enum.
1
2
3
4
5
6
7
enum Countries {
Ghana,
}
fn main() {
let country = Countries::Lesotho┃;
}
1
2
3
4
5
6
7
8
enum Countries {
Ghana,
Lesotho,
}
fn main() {
let country = Countries::Lesotho;
}
generate_fn_type_alias_named
Source: generate_fn_type_alias.rs
Generate a type alias for the function with named parameters.
1
unsafe fn fo┃o(n: i32) -> i32 { 42i32 }
1
2
3
type ${0:FooFn} = unsafe fn(n: i32) -> i32;
unsafe fn foo(n: i32) -> i32 { 42i32 }
generate_fn_type_alias_unnamed
Source: generate_fn_type_alias.rs
Generate a type alias for the function with unnamed parameters.
1
unsafe fn fo┃o(n: i32) -> i32 { 42i32 }
1
2
3
type ${0:FooFn} = unsafe fn(i32) -> i32;
unsafe fn foo(n: i32) -> i32 { 42i32 }
generate_from_impl_for_enum
Source: generate_from_impl_for_enum.rs
Adds a From impl for this enum variant with one tuple field.
1
enum A { ┃One(u32) }
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
Source: generate_function.rs
Adds a stub function with a signature matching the function under the cursor.
1
2
3
4
5
struct Baz;
fn baz() -> Baz { Baz }
fn foo() {
bar┃("", baz());
}
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
Source: generate_getter_or_setter.rs
Generate a getter method.
1
2
3
struct Person {
nam┃e: String,
}
1
2
3
4
5
6
7
8
9
struct Person {
name: String,
}
impl Person {
fn ┃name(&self) -> &str {
&self.name
}
}
generate_getter_mut
Source: generate_getter_or_setter.rs
Generate a mut getter method.
1
2
3
struct Person {
nam┃e: String,
}
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
Source: generate_impl.rs
Adds a new inherent impl for a type.
1
2
3
struct Ctx┃<T: Clone> {
data: T,
}
1
2
3
4
5
struct Ctx<T: Clone> {
data: T,
}
impl<T: Clone> Ctx<T> {┃}
generate_is_empty_from_len
Source: generate_is_empty_from_len.rs
Generates is_empty implementation from the len method.
1
2
3
4
5
6
7
8
struct MyStruct { data: Vec<String> }
impl MyStruct {
#[must_use]
p┃ub fn len(&self) -> usize {
self.data.len()
}
}
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
Source: generate_mut_trait_impl.rs
Adds a IndexMut impl from the Index
trait.
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]
}
}
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.
1
2
3
struct Ctx<T: Clone> {
data: T,┃
}
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
Source: generate_getter_or_setter.rs
Generate a setter method.
1
2
3
struct Person {
nam┃e: String,
}
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
Source: generate_trait_from_impl.rs
Generate trait for an already defined inherent impl and convert impl to a trait impl.
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> Fo┃o<N> {
// Used as an associated constant.
const CONST_ASSOC: usize = N * 4;
fn create() -> Option<()> {
Some(())
}
const_maker! {i32, 7}
}
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
Source: generate_impl.rs
Adds a new trait impl for a type.
1
2
3
struct ┃Ctx<T: Clone> {
data: T,
}
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.
1
2
3
fn foo(name: Option<&str>) {
let name = name.unwrap┃();
}
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
Source: inline_const_as_literal.rs
Evaluate and inline const variable as literal.
1
2
3
4
5
const STRING: &str = "Hello, World!";
fn something() -> &'static str {
STRING┃
}
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.
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("여러분");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fn print(_: &str) {}
fn bar() {
{
let word: &str = "안녕하세요";
if !word.is_empty() {
print(word);
}
};
{
let word: &str = "여러분";
if !word.is_empty() {
print(word);
}
};
}
inline_local_variable
Source: inline_local_variable.rs
Inlines a local variable.
1
2
3
4
fn main() {
let x┃ = 1 + 2;
x * 4;
}
1
2
3
fn main() {
(1 + 2) * 4;
}
inline_macro
Source: inline_macro.rs
Takes a macro and inlines it one step.
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}");
}
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
Source: inline_type_alias.rs
Replace a type alias with its concrete type.
1
2
3
4
5
type A<T = u32> = Vec<T>;
fn main() {
let a: ┃A;
}
1
2
3
4
5
type A<T = u32> = Vec<T>;
fn main() {
let a: Vec<u32>;
}
inline_type_alias_uses
Source: inline_type_alias.rs
Inline a type alias into all of its uses where possible.
1
2
3
4
5
6
7
type ┃A = i32;
fn id(x: A) -> A {
x
};
fn foo() {
let _: A = 3;
}
1
2
3
4
5
6
7
fn id(x: i32) -> i32 {
x
};
fn foo() {
let _: i32 = 3;
}
into_to_qualified_from
Source: into_to_qualified_from.rs
Convert an into
method call to a fully qualified from
call.
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.in┃to();
}
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
Source: introduce_named_generic.rs
Replaces impl Trait
function argument with the named generic.
1
fn foo(bar: ┃impl Bar) {}
1
fn foo<┃B: Bar>(bar: B) {}
introduce_named_lifetime
Source: introduce_named_lifetime.rs
Change an anonymous lifetime to a named lifetime.
1
2
3
4
5
6
7
impl Cursor<'_┃> {
fn node(self) -> &SyntaxNode {
match self {
Cursor::Replace(node) | Cursor::Before(node) => node,
}
}
}
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
.
1
2
3
fn main() {
if┃ !y { A } else { B }
}
1
2
3
fn main() {
if y { B } else { A }
}
line_to_block
Source: convert_comment_block.rs
Converts comments between block and single-line form.
1
2
// Multi-line┃
// comment
1
2
3
4
/*
Multi-line
comment
*/
make_raw_string
Source: raw_string.rs
Adds r#
to a plain string literal.
1
2
3
fn main() {
"Hello,┃ World!";
}
1
2
3
fn main() {
r#"Hello, World!"#;
}
make_usual_string
Source: raw_string.rs
Turns a raw string into a plain string.
1
2
3
fn main() {
r#"Hello,┃ "World!""#;
}
1
2
3
fn main() {
"Hello, \"World!\"";
}
merge_imports
Source: merge_imports.rs
Merges neighbor imports with a common prefix.
1
2
use std::┃fmt::Formatter;
use std::io;
1
use std::{fmt::Formatter, io};
merge_match_arms
Source: merge_match_arms.rs
Merges the current match arm with the following if their bodies are identical.
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(),
}
}
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
Source: merge_nested_if.rs
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
.
1
2
3
fn main() {
i┃f x == 3 { if y == 4 { 1 } }
}
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.
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() },
_ => (),
}
}
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.
1
2
3
fn apply<T, U, ┃F: FnOnce(T) -> U>(f: F, x: T) -> U {
f(x)
}
1
2
3
fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
f(x)
}
move_const_to_impl
Source: move_const_to_impl.rs
Move a local constant item in a method to impl’s associated constant. All the references will be
qualified with Self::
.
1
2
3
4
5
6
7
8
9
struct S;
impl S {
fn foo() -> usize {
/// The answer.
const C┃: usize = 42;
C * C
}
}
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
Source: move_from_mod_rs.rs
Moves xxx/mod.rs to xxx.rs.
1
2
3
4
//- /main.rs
mod a;
//- /a/mod.rs
┃fn t() {}┃
1
fn t() {}
move_guard_to_arm_body
Source: move_guard.rs
Moves match guard into match arm body.
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(),
_ => (),
}
}
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
Source: move_module_to_file.rs
Moves inline module’s contents to a separate file.
1
2
3
mod ┃foo {
fn t() {}
}
1
mod foo;
move_to_mod_rs
Source: move_to_mod_rs.rs
Moves xxx.rs to xxx/mod.rs.
1
2
3
4
//- /main.rs
mod a;
//- /a.rs
┃fn t() {}┃
1
fn t() {}
normalize_import
Source: normalize_import.rs
Normalizes an import.
1
use┃ std::{io, {fmt::Formatter}};
1
use std::{fmt::Formatter, io};
promote_local_to_const
Source: promote_local_to_const.rs
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.
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");
}
}
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
Source: pull_assignment_up.rs
Extracts variable assignment to outside an if or match statement.
1
2
3
4
5
6
7
8
9
fn main() {
let mut foo = 6;
if true {
┃foo = 5;
} else {
foo = 4;
}
}
1
2
3
4
5
6
7
8
9
fn main() {
let mut foo = 6;
foo = if true {
5
} else {
4
};
}
qualify_method_call
Source: qualify_method_call.rs
Replaces the method call with a qualified function call.
1
2
3
4
5
6
7
8
struct Foo;
impl Foo {
fn foo(&self) {}
}
fn main() {
let foo = Foo;
foo.fo┃o();
}
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.
1
2
3
fn main() {
let map = HashMap┃::new();
}
1
2
3
fn main() {
let map = std::collections::HashMap::new();
}
reformat_number_literal
Source: number_representation.rs
Adds or removes separators from integer literal.
1
const _: i32 = 1012345┃;
1
const _: i32 = 1_012_345;
remove_dbg
Source: remove_dbg.rs
Removes dbg!()
macro call.
1
2
3
fn main() {
let x = ┃dbg!(42 * dbg!(4 + 2));┃
}
1
2
3
fn main() {
let x = 42 * (4 + 2);
}
remove_hash
Source: raw_string.rs
Removes a hash from a raw string literal.
1
2
3
fn main() {
r#"Hello,┃ World!"#;
}
1
2
3
fn main() {
r"Hello, World!";
}
remove_mut
Source: remove_mut.rs
Removes the mut
keyword.
1
2
3
impl Walrus {
fn feed(&mut┃ self, amount: u32) {}
}
1
2
3
impl Walrus {
fn feed(&self, amount: u32) {}
}
remove_parentheses
Source: remove_parentheses.rs
Removes redundant parentheses.
1
2
3
fn main() {
_ = ┃(2) + 2;
}
1
2
3
fn main() {
_ = 2 + 2;
}
remove_unused_imports
Source: remove_unused_imports.rs
Removes any use statements in the current selection that are unused.
1
2
3
4
struct X();
mod foo {
use super::X┃;
}
1
2
3
struct X();
mod foo {
}
remove_unused_param
Source: remove_unused_param.rs
Removes unused function parameter.
1
2
3
4
5
fn frobnicate(x: i32┃) {}
fn main() {
frobnicate(92);
}
1
2
3
4
5
fn frobnicate() {}
fn main() {
frobnicate();
}
reorder_fields
Source: reorder_fields.rs
Reorder the fields of record literals and record patterns in the same order as in the definition.
1
2
struct Foo {foo: i32, bar: i32};
const test: Foo = ┃Foo {bar: 0, foo: 1}
1
2
struct Foo {foo: i32, bar: i32};
const test: Foo = Foo {foo: 1, bar: 0}
reorder_impl_items
Source: reorder_impl_items.rs
Reorder the items of an impl Trait
. The items will be ordered
in the same order as in the trait definition.
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;
}
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
Source: replace_arith_op.rs
Replaces arithmetic on integers with the checked_*
equivalent.
1
2
3
fn main() {
let x = 1 ┃+ 2;
}
1
2
3
fn main() {
let x = 1.checked_add(2);
}
replace_arith_with_saturating
Source: replace_arith_op.rs
Replaces arithmetic on integers with the saturating_*
equivalent.
1
2
3
fn main() {
let x = 1 ┃+ 2;
}
1
2
3
fn main() {
let x = 1.saturating_add(2);
}
replace_arith_with_wrapping
Source: replace_arith_op.rs
Replaces arithmetic on integers with the wrapping_*
equivalent.
1
2
3
fn main() {
let x = 1 ┃+ 2;
}
1
2
3
fn main() {
let x = 1.wrapping_add(2);
}
replace_char_with_string
Source: replace_string_with_char.rs
Replace a char literal with a string literal.
1
2
3
fn main() {
find('{┃');
}
1
2
3
fn main() {
find("{");
}
replace_derive_with_manual_impl
Converts a derive
impl into a manual one.
1
2
#[derive(Deb┃ug, Display)]
struct S;
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
Source: replace_if_let_with_match.rs
Replaces a if let
expression with a match
expression.
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()
}
}
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
.
1
2
3
4
fn main() {
let x = Some(1);
if x.is_som┃e() {}
}
1
2
3
4
fn main() {
let x = Some(1);
if let Some(${0:x}) = x {}
}
replace_let_with_if_let
Source: replace_let_with_if_let.rs
Replaces let
with an if let
.
1
2
3
4
5
6
fn main(action: Action) {
┃let x = compute();
}
fn compute() -> Option<i32> { None }
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
Source: replace_if_let_with_match.rs
Replaces a binary match
with a wildcard pattern and no guards with an if let
expression.
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(),
}
}
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.
1
fn new<P┃: AsRef<Path>>(location: P) -> Self {}
1
fn new(location: impl AsRef<Path>) -> Self {}
replace_qualified_name_with_use
Adds a use statement for a given fully-qualified name.
1
fn process(map: std::collections::┃HashMap<String, String>) {}
1
2
3
use std::collections::HashMap;
fn process(map: HashMap<String, String>) {}
replace_string_with_char
Source: replace_string_with_char.rs
Replace string literal with char literal.
1
2
3
fn main() {
find("{┃");
}
1
2
3
fn main() {
find('{');
}
replace_try_expr_with_match
Source: replace_try_expr_with_match.rs
Replaces a try
expression with a match
expression.
1
2
3
fn handle() {
let pat = Some(true)┃?;
}
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.
1
2
3
4
fn make<T>() -> T { ) }
fn main() {
let a = make┃::<i32>();
}
1
2
3
4
fn make<T>() -> T { ) }
fn main() {
let a: i32 = make();
}
replace_with_eager_method
Source: replace_method_eager_lazy.rs
Replace unwrap_or_else
with unwrap_or
and ok_or_else
with ok_or
.
1
2
3
4
fn foo() {
let a = Some(1);
a.unwra┃p_or_else(|| 2);
}
1
2
3
4
fn foo() {
let a = Some(1);
a.unwrap_or(2);
}
replace_with_lazy_method
Source: replace_method_eager_lazy.rs
Replace unwrap_or
with unwrap_or_else
and ok_or
with ok_or_else
.
1
2
3
4
fn foo() {
let a = Some(1);
a.unwra┃p_or(2);
}
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.
1
struct ┃Foo┃ { second: u32, first: String }
1
struct Foo { first: String, second: u32 }
1
2
3
4
trait ┃Bar┃ {
fn second(&self) -> u32;
fn first(&self) -> String;
}
1
2
3
4
trait Bar {
fn first(&self) -> String;
fn second(&self) -> u32;
}
1
2
3
4
5
struct Baz;
impl ┃Baz┃ {
fn second(&self) -> u32;
fn first(&self) -> String;
}
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:
1
2
3
4
enum ┃Animal┃ {
Dog(String, f64),
Cat { weight: f64, name: String },
}
1
2
3
4
enum Animal {
Cat { weight: f64, name: String },
Dog(String, f64),
}
and sorting a single enum struct variant:
1
2
3
4
enum Animal {
Dog(String, f64),
Cat ┃{ weight: f64, name: String }┃,
}
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.
1
use std::┃collections::HashMap;
1
use std::{collections::HashMap};
sugar_impl_future_into_async
Source: toggle_async_sugar.rs
Rewrites asynchronous function from → impl Future
into async fn
.
This action does not touch the function body and therefore async { 0 }
block does not transform to just 0
.
1
2
3
pub fn foo() -> impl core::future::F┃uture<Output = usize> {
async { 0 }
}
1
2
3
pub async fn foo() -> usize {
async { 0 }
}
toggle_ignore
Source: toggle_ignore.rs
Adds #[ignore]
attribute to the test.
1
2
3
4
┃#[test]
fn arithmetics {
assert_eq!(2 + 2, 5);
}
1
2
3
4
5
#[test]
#[ignore]
fn arithmetics {
assert_eq!(2 + 2, 5);
}
toggle_macro_delimiter
Source: toggle_macro_delimiter.rs
Change macro delimiters in the order of ( → { → [ → (
.
1
2
3
4
5
macro_rules! sth {
() => {};
}
sth!┃( );
1
2
3
4
5
macro_rules! sth {
() => {};
}
sth!{ }
unmerge_match_arm
Source: unmerge_match_arm.rs
Splits the current match with a |
pattern into two arms with identical bodies.
1
2
3
4
5
6
7
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move(..) ┃| Action::Stop => foo(),
}
}
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.
1
use std::fmt::{Debug, Display┃};
1
2
use std::fmt::{Debug};
use std::fmt::Display;
unnecessary_async
Source: unnecessary_async.rs
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.
1
2
pub async f┃n foo() {}
pub async fn bar() { foo().await }
1
2
pub fn foo() {}
pub async fn bar() { foo() }
unqualify_method_call
Source: unqualify_method_call.rs
Transforms universal function call syntax into a method call.
1
2
3
fn main() {
std::ops::Add::add┃(1, 2);
}
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.
1
2
3
4
5
fn foo() {
if true {┃
println!("foo");
}
}
1
2
3
fn foo() {
println!("foo");
}
unwrap_option_return_type
Source: unwrap_return_type.rs
Unwrap the function’s return type.
1
fn foo() -> Option<i32>┃ { Some(42i32) }
1
fn foo() -> i32 { 42i32 }
unwrap_result_return_type
Source: unwrap_return_type.rs
Unwrap the function’s return type.
1
fn foo() -> Result<i32>┃ { Ok(42i32) }
1
fn foo() -> i32 { 42i32 }
unwrap_tuple
Source: unwrap_tuple.rs
Unwrap the tuple to different variables.
1
2
3
fn main() {
┃let (foo, bar) = ("Foo", "Bar");
}
1
2
3
4
fn main() {
let foo = "Foo";
let bar = "Bar";
}
wrap_return_type_in_option
Source: wrap_return_type.rs
Wrap the function’s return type into Option.
1
fn foo() -> i32┃ { 42i32 }
1
fn foo() -> Option<i32> { Some(42i32) }
wrap_return_type_in_result
Source: wrap_return_type.rs
Wrap the function’s return type into Result.
1
fn foo() -> i32┃ { 42i32 }
1
fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }
wrap_unwrap_cfg_attr
Source: wrap_unwrap_cfg_attr.rs
Wraps an attribute to a cfg_attr attribute or unwraps a cfg_attr attribute to the inner attributes.
1
2
3
4
#[derive┃(Debug)]
struct S {
field: i32
}
1
2
3
4
#[cfg_attr(┃, derive(Debug))]
struct S {
field: i32
}
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"
.
await-outside-of-async
Source: await_outside_of_async.rs
This diagnostic is triggered if the await
keyword is used outside of an async function or block
break-outside-of-loop
Source: break_outside_of_loop.rs
This diagnostic is triggered if the break
keyword is used outside of a loop.
cast-to-unsized
Source: invalid_cast.rs
This diagnostic is triggered when casting to an unsized type
expected-function
Source: expected_function.rs
This diagnostic is triggered if a call is made on something that is not callable.
generic-args-prohibited
Source: generic_args_prohibited.rs
This diagnostic is shown when generic arguments are provided for a type that does not accept generic arguments.
inactive-code
Source: inactive_code.rs
This diagnostic is shown for code with inactive #[cfg]
attributes.
incoherent-impl
Source: incoherent_impl.rs
This diagnostic is triggered if the targe type of an impl is from a foreign crate.
incorrect-ident-case
Source: incorrect_case.rs
This diagnostic is triggered if an item name doesn’t follow Rust naming convention.
invalid-cast
Source: invalid_cast.rs
This diagnostic is triggered if the code contains an illegal cast
invalid-derive-target
Source: invalid_derive_target.rs
This diagnostic is shown when the derive attribute is used on an item other than a struct
,
enum
or union
.
malformed-derive
Source: malformed_derive.rs
This diagnostic is shown when the derive attribute has invalid input.
mismatched-arg-count
Source: mismatched_arg_count.rs
This diagnostic is triggered if a function is invoked with an incorrect amount of arguments.
mismatched-tuple-struct-pat-arg-count
Source: mismatched_arg_count.rs
This diagnostic is triggered if a function is invoked with an incorrect amount of arguments.
missing-fields
Source: missing_fields.rs
This diagnostic is triggered if record lacks some fields that exist in the corresponding structure.
Example:
1
2
3
struct A { a: u8, b: u8 }
let a = A { a: 10 };
missing-match-arm
Source: missing_match_arms.rs
This diagnostic is triggered if match
block is missing one or more match arms.
missing-unsafe
Source: missing_unsafe.rs
This diagnostic is triggered if an operation marked as unsafe
is used outside of an unsafe
function or block.
moved-out-of-ref
Source: moved_out_of_ref.rs
This diagnostic is triggered on moving non copy things out of references.
need-mut
Source: mutability_errors.rs
This diagnostic is triggered on mutating an immutable variable.
no-such-field
Source: no_such_field.rs
This diagnostic is triggered if created structure does not have field provided in record.
non-exhaustive-let
Source: non_exhaustive_let.rs
This diagnostic is triggered if a let
statement without an else
branch has a non-exhaustive
pattern.
private-assoc-item
Source: private_assoc_item.rs
This diagnostic is triggered if the referenced associated item is not visible from the current module.
private-field
Source: private_field.rs
This diagnostic is triggered if the accessed field is not visible from the current module.
proc-macro-disabled
Source: macro_error.rs
This diagnostic is shown for proc macros that has been specifically disabled via rust-analyzer.procMacro.ignored
.
proc-macros-disabled
Source: macro_error.rs
This diagnostic is shown for proc macros where proc macros have been disabled.
remove-trailing-return
Source: remove_trailing_return.rs
This diagnostic is triggered when there is a redundant return
at the end of a function
or closure.
remove-unnecessary-else
Source: remove_unnecessary_else.rs
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
Source: trait_impl_incorrect_safety.rs
Diagnoses incorrect safety annotations of trait impls.
trait-impl-missing-assoc_item
Source: trait_impl_missing_assoc_item.rs
Diagnoses missing trait items in a trait impl.
trait-impl-orphan
Source: trait_impl_orphan.rs
Only traits defined in the current crate can be implemented for arbitrary types
type-mismatch
Source: type_mismatch.rs
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
Source: undeclared_label.rs
unimplemented-builtin-macro
Source: unimplemented_builtin_macro.rs
This diagnostic is shown for builtin macros which are not yet implemented by rust-analyzer
unlinked-file
Source: unlinked_file.rs
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.
unreachable-label
Source: unreachable_label.rs
unresolved-assoc-item
Source: unresolved_assoc_item.rs
This diagnostic is triggered if the referenced associated item does not exist.
unresolved-extern-crate
Source: unresolved_extern_crate.rs
This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate.
unresolved-field
Source: unresolved_field.rs
This diagnostic is triggered if a field does not exist on a given type.
unresolved-ident
Source: unresolved_ident.rs
This diagnostic is triggered if an expr-position ident is invalid.
unresolved-import
Source: unresolved_import.rs
This diagnostic is triggered if rust-analyzer is unable to resolve a path in
a use
declaration.
unresolved-macro-call
Source: unresolved_macro_call.rs
This diagnostic is triggered if rust-analyzer is unable to resolve the path to a macro in a macro invocation.
unresolved-method
Source: unresolved_method.rs
This diagnostic is triggered if a method does not exist on a given type.
unresolved-module
Source: unresolved_module.rs
This diagnostic is triggered if rust-analyzer is unable to discover referred module.
unused-mut
Source: mutability_errors.rs
This diagnostic is triggered when a mutable variable isn’t actually mutated.
unused-variables
Source: unused_variables.rs
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.