Skip to content

Rust#

Server#

For more information about the LSP server, check rust-analyzer.

Installation#

For more instructions on how to install manually, check here.

Debugger: Not available#


author: yyoncho template: comment.html root_file: docs/manual-language-docs/lsp-rust-analyzer.md


Server note#

NOTE: If you are using rustic-mode, you have to change rustic-lsp-server instead of lsp-rust-server, since it also supports eglot as a lightweight alternative to lsp-mode.

  • lsp-rust-server Choose LSP server (default is rust-analyzer)

  • lsp-rust-switch-server Switch priorities of lsp servers

rust-analyzer#

Commands#

lsp-rust-analyzer-syntax-tree#

Display syntax tree for current buffer

lsp-rust-analyzer-status#

Display status information for rust-analyzer

lsp-rust-analyzer-join-lines#

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

before:

after:

inlay-hints#

lsp-inlay-hints-mode enables displaying of inlay hints

Additionally, lsp-inlay-hint-enable must be set to t in order for inlay hints to render.

NOTE: the inlay hints interact badly with the lsp-ui sideline, because it doesn't seem to consider the overlays in its width calculation, which often leads to lines wrapping around.

Macro expansion#

lsp-rust-analyzer-expand-macro expand macro call at point recursively

Use your own function for displaying macro expansion by customizing lsp-rust-analyzer-macro-expansion-method

Formatted and highlighted result with the default function of rustic.

auto-import#

Get a list of possible auto import candidates with lsp-execute-code-action

Snippet insertion/refactor#

To support refactorings that require snippet insertion(eg. generating derive clause etc), make sure that you have enabled yasnippet and yas-minor-mode. If you are using use-package, you can do something like this:

(use-package yasnippet
  :ensure t
  :hook ((lsp-mode . yas-minor-mode)))

Open Cargo.toml#

lsp-rust-analyzer-open-cargo-toml opens the Cargo.toml closest to the current file. Calling it with a universal argument will open the Cargo.toml in another window.

Corresponds to the rust-analyzer LSP extension

Open external documentation#

lsp-rust-analyzer-open-external-docs opens external documentation related to the current position in a browser.

Corresponds to the rust-analyzer LSP extension

lsp-rust-analyzer-related-tests find all tests related to the current position, asks for user completion and executes the selected test in a compilation buffer.

Corresponds to the rust-analyzer LSP extension

In the example below, first you see that: + On the left, the function check_infer is defined, on the right another file is opened with many test functions, some of which call check_infer. With the cursor on check_infer, call lsp-rust-analyzer-related-tests and select infer_pattern_match_slice with fuzzy matching. The test is executed on the right with compilation major mode

  • Move the cursor to fn ellipsize and attempt to find related tests to no avail. Confirm that the function is indeed untested by using swiper and finding one place in the file, where the function is called

Caveats#

  • Rust Analyzer does not support disabling snippets - https://github.com/rust-analyzer/rust-analyzer/issues/2518

extract signature#

This unmerged PR contains an example method that allows modifying the signature that is displayed by eldoc.

Available configurations#

lsp-rust-server#

Type: (choice (const :tag rls rls) (const :tag rust-analyzer rust-analyzer))

Default: rust-analyzer

Choose LSP server.


lsp-rust-analyzer-binding-mode-hints#

Type: boolean

Default: nil

Whether to show inlay type hints for binding modes.


lsp-rust-analyzer-call-info-full#

Type: boolean

Default: t

Whether to show function name and docs in parameter hints.


lsp-rust-analyzer-cargo-auto-reload#

Type: boolean

Default: t

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


lsp-rust-analyzer-cargo-extra-args#

Type: lsp-string-vector

Default: []

Extra arguments that are passed to every cargo invocation.


lsp-rust-analyzer-cargo-extra-env#

Type: lsp-string-vector

Default: []

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


lsp-rust-analyzer-cargo-override-command#

Type: lsp-string-vector

Default: []

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


lsp-rust-analyzer-cargo-run-build-scripts#

Type: boolean

Default: t

Whether to run build scripts (build.rs) for more precise code analysis.


lsp-rust-analyzer-cargo-target#

Type: (choice (string :tag Target) (const :tag None nil))

Default: nil

Compilation target (target triple).


lsp-rust-analyzer-cargo-unset-test#

Type: lsp-string-vector

Default: []

force rust-analyzer to unset #[cfg(test)] for the specified crates.


lsp-rust-analyzer-cargo-watch-args#

Type: lsp-string-vector

Default: []

Extra arguments for cargo check.


lsp-rust-analyzer-cargo-watch-command#

Type: string

Default: check

Cargo watch command.


lsp-rust-analyzer-cargo-watch-enable#

Type: boolean

Default: t

Enable Cargo watch.


lsp-rust-analyzer-check-all-targets#

Type: boolean

Default: t

Enables --all-targets for cargo check.


lsp-rust-analyzer-closing-brace-hints#

Type: boolean

Default: t

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


lsp-rust-analyzer-closing-brace-hints-min-lines#

Type: integer

Default: 25

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


lsp-rust-analyzer-closure-capture-hints#

Type: boolean

Default: nil

Whether to show inlay hints for closure captures.


lsp-rust-analyzer-closure-return-type-hints#

Type: (choice (const never) (const always) (const with_block))

Default: never

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


lsp-rust-analyzer-closure-style#

Type: string

Default: impl_fn

Closure notation in type and chaining inlay hints.


lsp-rust-analyzer-completion-add-call-argument-snippets#

Type: boolean

Default: t

Whether to add argument snippets when completing functions.


lsp-rust-analyzer-completion-add-call-parenthesis#

Type: boolean

Default: t

Whether to add parenthesis when completing functions.


lsp-rust-analyzer-completion-auto-import-enable#

Type: boolean

Default: t

Toggles the additional completions that automatically add imports when completed. lsp-completion-enable-additional-text-edit must be non-nil for this feature to be fully enabled.


lsp-rust-analyzer-completion-auto-self-enable#

Type: boolean

Default: t

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


lsp-rust-analyzer-completion-postfix-enable#

Type: boolean

Default: t

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


lsp-rust-analyzer-debug-lens-extra-dap-args#

Type: plist

Default: (:MIMode gdb :miDebuggerPath gdb :stopAtEntry t :externalConsole :json-false)

Extra arguments to pass to DAP template when debugging a test from code lens.

As a rule of the thumb, do not add extra keys to this plist unless you exactly what you are doing, it might break the "Debug test" lens otherwise.

See dap-mode documentation and cpptools documentation for the extra variables meaning.


lsp-rust-analyzer-diagnostics-disabled#

Type: lsp-string-vector

Default: []

List of native rust-analyzer diagnostics to disable.


lsp-rust-analyzer-diagnostics-enable#

Type: boolean

Default: t

Whether to show native rust-analyzer diagnostics.


lsp-rust-analyzer-diagnostics-enable-experimental#

Type: boolean

Default: nil

Whether to show native rust-analyzer diagnostics that are still experimental (might have more false positives than usual).


lsp-rust-analyzer-diagnostics-warnings-as-hint#

Type: lsp-string-vector

Default: []

List of warnings that should be displayed with hint severity.


lsp-rust-analyzer-diagnostics-warnings-as-info#

Type: lsp-string-vector

Default: []

List of warnings that should be displayed with info severity.


lsp-rust-analyzer-discriminants-hints#

Type: (choice (const never) (const always) (const fieldless))

Default: never

Whether to show enum variant discriminant hints.


lsp-rust-analyzer-display-chaining-hints#

Type: boolean

Default: nil

Whether to show inlay type hints for method chains. These hints will be formatted with the type hint formatting options, if the mode is not configured to ask the server to format them.


lsp-rust-analyzer-display-closure-return-type-hints#

Type: boolean

Default: nil

Whether to show closure return type inlay hints for closures with block bodies.


lsp-rust-analyzer-display-lifetime-elision-hints-enable#

Type: (choice (const never) (const always) (const skip_trivial))

Default: never

Whether to show elided lifetime inlay hints.


lsp-rust-analyzer-display-lifetime-elision-hints-use-parameter-names#

Type: boolean

Default: nil

When showing elided lifetime inlay hints, whether to use parameter names or numeric placeholder names for the lifetimes.


lsp-rust-analyzer-display-parameter-hints#

Type: boolean

Default: nil

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


lsp-rust-analyzer-display-reborrow-hints#

Type: (choice (const always) (const never) (const mutable))

Default: never

Whether to show inlay type hints for compiler inserted reborrows.


lsp-rust-analyzer-download-url#

Type: string

Default: https://github.com/rust-lang/rust-analyzer/releases/latest/download/rust-analyzer-x86_64-unknown-linux-gnu.gz

Automatic download url for Rust Analyzer


lsp-rust-analyzer-exclude-dirs#

Type: lsp-string-vector

Default: []

These directories will be ignored by rust-analyzer.


lsp-rust-analyzer-exclude-globs#

Type: lsp-string-vector

Default: []

Exclude globs


lsp-rust-analyzer-experimental-proc-attr-macros#

Type: boolean

Default: t

Whether to enable experimental support for expanding proc macro attributes.


lsp-rust-analyzer-expression-adjustment-hide-unsafe#

Type: boolean

Default: nil

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


lsp-rust-analyzer-expression-adjustment-hints#

Type: (choice (const never) (const always) (const reborrow))

Default: never

Whether to show inlay hints for type adjustments..


lsp-rust-analyzer-expression-adjustment-hints-mode#

Type: (choice (const prefix) (const postfix) (const prefer_prefix) (const prefer_postfix))

Default: prefix

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


lsp-rust-analyzer-hide-closure-initialization#

Type: boolean

Default: nil

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


lsp-rust-analyzer-hide-named-constructor#

Type: boolean

Default: nil

Whether to hide inlay type hints for constructors.


lsp-rust-analyzer-highlight-breakpoints#

Type: boolean

Default: t

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


lsp-rust-analyzer-highlight-closure-captures#

Type: boolean

Default: t

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


lsp-rust-analyzer-highlight-exit-points#

Type: boolean

Default: t

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


lsp-rust-analyzer-highlight-references#

Type: boolean

Default: t

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


lsp-rust-analyzer-highlight-yield-points#

Type: boolean

Default: t

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


lsp-rust-analyzer-highlighting-strings#

Type: boolean

Default: t

Use semantic tokens for strings.


lsp-rust-analyzer-implicit-drops#

Type: boolean

Default: nil

Whether to show implicit drop hints.


lsp-rust-analyzer-import-enforce-granularity#

Type: boolean

Default: nil

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


lsp-rust-analyzer-import-granularity#

Type: (choice (const crate :doc 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.) (const module :doc Merge imports from the same module into a single use statement.) (const item :doc Don’t merge imports at all, creating one import per item.) (const preserve :doc Do not change the granularity of any imports. For auto-import this has the same effect as"item"'))`

Default: crate

How imports should be grouped into use statements.


lsp-rust-analyzer-import-group#

Type: boolean

Default: t

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


lsp-rust-analyzer-import-prefix#

Type: (choice (const plain) (const by_self) (const by_crate))

Default: plain

The path structure for newly inserted paths to use. Valid values are: - "plain": Insert import paths relative to the current module, using up to one super prefix if the parent module contains the requested item. - "by_self": Prefix all import paths with self if they dont begin withself,super,crateor a crate name. - "by_crate": Force import paths to be absolute by always starting them withcrate` or the crate name they refer to.


lsp-rust-analyzer-imports-merge-glob#

Type: boolean

Default: t

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


lsp-rust-analyzer-library-directories#

Type: (repeat string)

Default: (~/.cargo/registry/src ~/.rustup/toolchains)

List of directories which will be considered to be libraries.


lsp-rust-analyzer-linked-projects#

Type: lsp-string-vector

Default: []

Disable project auto-discovery in favor of explicitly specified set of projects. Elements must be paths pointing to Cargo.toml, rust-project.json, or JSON objects in rust-project.json format.


lsp-rust-analyzer-lru-capacity#

Type: integer

Default: nil

Number of syntax trees rust-analyzer keeps in memory.


lsp-rust-analyzer-macro-expansion-method#

Type: function

Default: lsp-rust-analyzer-macro-expansion-default

Use a different function if you want formatted macro expansion results and syntax highlighting.


lsp-rust-analyzer-max-inlay-hint-length#

Type: integer

Default: nil

Max inlay hint length.


lsp-rust-analyzer-proc-macro-enable#

Type: boolean

Default: t

Enable Proc macro support. Implies lsp-rust-analyzer-cargo-run-build-scripts


lsp-rust-analyzer-rustc-source#

Type: (choice (file :tag Path) (const :tag None nil))

Default: nil

Path to the Cargo.toml of the rust compiler workspace.


lsp-rust-analyzer-rustfmt-extra-args#

Type: lsp-string-vector

Default: []

Additional arguments to rustfmt.


lsp-rust-analyzer-rustfmt-override-command#

Type: lsp-string-vector

Default: []

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


lsp-rust-analyzer-rustfmt-rangeformatting-enable#

Type: boolean

Default: nil

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


lsp-rust-analyzer-server-command#

Type: (repeat string)

Default: (rust-analyzer)

Command to start rust-analyzer.


lsp-rust-analyzer-server-format-inlay-hints#

Type: boolean

Default: t

Whether to ask rust-analyzer to format inlay hints itself. If active, the various inlay format settings are not used.


lsp-rust-analyzer-store-path#

Type: file

Default: ~/.emacs.d/.cache/lsp/rust/rust-analyzer

The path to the file in which rust-analyzer will be stored.


lsp-rust-analyzer-use-client-watching#

Type: boolean

Default: t

Use client watching


lsp-rust-analyzer-use-rustc-wrapper-for-build-scripts#

Type: boolean

Default: t

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



Last update: October 7, 2024