Skip to content

Rust#

Server#

For more information about the LSP server, check rls.

Installation#

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

Debugger: Yes#

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 RLS)

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

RLS#

Customization#

This is an incomplete list of the available options

  • lsp-rust-rls-server-command change command to start RLS

  • lsp-rust-show-hover-context turn off hover tooltips

rustfmt#

Code formatting with rustfmt can be configured with:

lsp-rust-rustfmt-path change default path for rustfmt executable

To enable automatic code format on save, add this to your init.el (rust-mode is assumed to be installed):

(add-hook 'before-save-hook (lambda () (when (eq 'rust-mode major-mode)
                                           (lsp-format-buffer))))

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-rust-analyzer-inlay-hints-mode enables displaying of inlay hints

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

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#

Default: rust-analyzer

Choose LSP server.


lsp-rust-rls-server-command#

Default: (rls)

Command to start RLS.


lsp-rust-library-directories#

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

List of directories which will be considered to be libraries.


lsp-rust-sysroot#

Default: nil

If non-nil, use the given path as the sysroot for all rustc invocations instead of trying to detect the sysroot automatically.


lsp-rust-target#

Default: nil

If non-nil, use the given target triple for all rustc invocations.


lsp-rust-rustflags#

Default: nil

Flags added to RUSTFLAGS.


lsp-rust-clear-env-rust-log#

Default: t

Clear the RUST_LOG environment variable before running rustc or cargo.


lsp-rust-build-lib#

Default: nil

If non-nil, checks the project as if you passed the ‘--lib’ argument to cargo.

Mutually exclusive with, and preferred over, ‘lsp-rust-build-bin’. (Unstable)


lsp-rust-build-bin#

Default: nil

If non-nil, checks the project as if you passed ‘-- bin ’ argument to cargo.

Mutually exclusive with ‘lsp-rust-build-lib’. (Unstable)


lsp-rust-cfg-test#

Default: nil

If non-nil, checks the project as if you were running ‘cargo test’ rather than cargo build.

I.e., compiles (but does not run) test code.


lsp-rust-unstable-features#

Default: nil

Enable unstable features.


lsp-rust-wait-to-build#

Default: nil

Time in milliseconds between receiving a change notification and starting build. If not specified, automatically inferred by the latest build duration.


lsp-rust-show-warnings#

Default: t

Show warnings.


lsp-rust-crate-blacklist#

Default: [cocoa gleam glium idna libc openssl rustc_serialize serde serde_json typenum unicode_normalization unicode_segmentation winapi]

A list of Cargo crates to blacklist.


lsp-rust-build-on-save#

Default: nil

Only index the project when a file is saved and not on change.


lsp-rust-features#

Default: []

List of Cargo features to enable.


lsp-rust-all-features#

Default: nil

Enable all Cargo features.


lsp-rust-no-default-features#

Default: nil

Do not enable default Cargo features.


lsp-rust-racer-completion#

Default: t

Enables code completion using racer.


lsp-rust-clippy-preference#

Default: opt-in

Controls eagerness of clippy diagnostics when available. Valid values are (case-insensitive): - "off": Disable clippy lints. - "opt-in": Clippy lints are shown when crates specify ‘#![warn(clippy)]‘. - "on": Clippy lints enabled for all crates in workspace. You need to install clippy via rustup if you haven’t already.


lsp-rust-jobs#

Default: nil

Number of Cargo jobs to be run in parallel.


lsp-rust-all-targets#

Default: t

Checks the project as if you were running cargo check --all-targets. I.e., check all targets and integration tests too.


lsp-rust-target-dir#

Default: nil

When specified, it places the generated analysis files at the specified target directory. By default it is placed target/rls directory.


lsp-rust-rustfmt-path#

Default: nil

When specified, RLS will use the Rustfmt pointed at the path instead of the bundled one


lsp-rust-build-command#

Default: nil

EXPERIMENTAL (requires ‘unstable_features‘) If set, executes a given program responsible for rebuilding save-analysis to be loaded by the RLS. The program given should output a list of resulting .json files on stdout. Implies ‘rust.build_on_save‘: true.


lsp-rust-full-docs#

Default: nil

Instructs cargo to enable full documentation extraction during save-analysis while building the crate.


lsp-rust-show-hover-context#

Default: t

Show additional context in hover tooltips when available. This is often the type local variable declaration.


lsp-rust-analyzer-server-command#

Default: (rust-analyzer)

Command to start rust-analyzer.


lsp-rust-analyzer-server-args#

Default: nil

Args to start rust-analyzer with when rust-analyzer is downloaded automatically.


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

Default: nil

Show inlay hints.


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

Default: nil

Max inlay hint length.


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

Default: nil

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


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

Default: nil

Whether to show inlay type hints for method chains.


lsp-rust-analyzer-lru-capacity#

Default: nil

Number of syntax trees rust-analyzer keeps in memory.


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

Default: t

Enable Cargo watch.


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

Default: check

Cargo watch command.


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

Default: []

Cargo watch args.


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

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-all-targets#

Default: t

Cargo watch all targets or not.


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

Default: t

Use client watching


lsp-rust-analyzer-exclude-globs#

Default: []

Exclude globs


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

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

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


lsp-rust-analyzer-diagnostics-enable#

Default: t

Whether to show native rust-analyzer diagnostics.


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

Default: t

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


lsp-rust-analyzer-diagnostics-disabled#

Default: []

List of native rust-analyzer diagnostics to disable.


lsp-rust-analyzer-cargo-load-out-dirs-from-check#

Default: nil

Whether to run ‘cargo check‘ on startup to get the correct value for package OUT_DIRs.


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

Default: []

Additional arguments to rustfmt.


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

Default: []

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


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

Default: t

Whether to add parenthesis when completing functions.


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

Default: t

Whether to add argument snippets when completing functions.


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

Default: t

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


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

Default: t

Whether to show function name and docs in parameter hints.


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

Default: nil

Enable Proc macro support; lsp-rust-analyzer-cargo-load-out-dirs-from-check must be enabled.


lsp-rust-analyzer-import-merge-behaviour#

Default: full

The strategy to use when inserting new imports or merging imports. Valid values are: - "none": No merging - "full": Merge all layers of the import trees - "last": Only merge the last layer of the import trees


lsp-rust-analyzer-import-prefix#

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 don’t begin with ‘self‘, ‘super‘, ‘crate‘ or a crate name - "by_crate": Force import paths to be absolute by always starting them with ‘crate‘ or the crate name they refer to.


lsp-rust-analyzer-download-url#

Default: https://github.com/rust-analyzer/rust-analyzer/releases/latest/download/rust-analyzer-linux

Automatic download url for Rust Analyzer


lsp-rust-analyzer-store-path#

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

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


lsp-rust-analyzer-inlay-type-space-format#

Default: %s

Format string for spacing around variable inlays (not part of the inlay face).


lsp-rust-analyzer-inlay-type-format#

Default: : %s

Format string for variable inlays (part of the inlay face).


lsp-rust-analyzer-inlay-param-space-format#

Default: %s

Format string for spacing around parameter inlays (not part of the inlay face).


lsp-rust-analyzer-inlay-param-format#

Default: %s:

Format string for parameter inlays (part of the inlay face).


lsp-rust-analyzer-inlay-chain-space-format#

Default: %s

Format string for spacing around chain inlays (not part of the inlay face).


lsp-rust-analyzer-inlay-chain-format#

Default: : %s

Format string for chain inlays (part of the inlay face).



Last update: February 23, 2021