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 rust-analyzer)

  • 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

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

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.

TRAMP Example#

The following is an example configuration for using lsp-mode with a remote rust-analyzer server:

(with-eval-after-load "lsp-rust"
  (lsp-register-client
   (make-lsp-client
    :new-connection (lsp-tramp-connection "rust-analyzer")
    :remote? t
    :major-modes '(rust-mode rustic-mode)
    :initialization-options 'lsp-rust-analyzer--make-init-options
    :notification-handlers (ht<-alist lsp-rust-notification-handlers)
    :action-handlers (ht ("rust-analyzer.runSingle" #'lsp-rust--analyzer-run-single))
    :library-folders-fn (lambda (_workspace) lsp-rust-library-directories)
    :after-open-fn (lambda ()
                     (when lsp-rust-analyzer-server-display-inlay-hints
                       (lsp-rust-analyzer-inlay-hints-mode)))
    :ignore-messages nil
    :server-id 'rust-analyzer-remote)))

Available configurations#

lsp-rust-all-features#

Type: boolean

Default: nil

Enable all Cargo features.


lsp-rust-all-targets#

Type: boolean

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-analyzer-call-info-full#

Type: boolean

Default: t

Whether to show function name and docs in parameter hints.


lsp-rust-analyzer-cargo-all-targets#

Type: boolean

Default: t

Cargo watch all targets or not.


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-watch-args#

Type: lsp-string-vector

Default: []

Cargo watch args.


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-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-postfix-enable#

Type: boolean

Default: t

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


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: t

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


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

Type: boolean

Default: nil

Whether to show inlay type hints for method chains.


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-download-url#

Type: string

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

Automatic download url for Rust Analyzer


lsp-rust-analyzer-exclude-globs#

Type: lsp-string-vector

Default: []

Exclude globs


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

Type: (choice (const none) (const full) (const last))

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#

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-inlay-chain-face#

Type: face

Default: ((t :inherit lsp-rust-analyzer-inlay-face))

Face for inlay chaining hints (e.g. inferred chain intermediate types).


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

Type: (string :tag String)

Default: : %s

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


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

Type: (string :tag String)

Default: %s

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


lsp-rust-analyzer-inlay-face#

Type: face

Default: ((t :inherit font-lock-comment-face))

The face to use for the Rust Analyzer inlays.


lsp-rust-analyzer-inlay-param-face#

Type: face

Default: ((t :inherit lsp-rust-analyzer-inlay-face))

Face for inlay parameter hints (e.g. function parameter names at call-site).


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

Type: (string :tag String)

Default: %s:

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


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

Type: (string :tag String)

Default: %s

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


lsp-rust-analyzer-inlay-type-face#

Type: face

Default: ((t :inherit lsp-rust-analyzer-inlay-face))

Face for inlay type hints (e.g. inferred variable types).


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

Type: (string :tag String)

Default: : %s

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


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

Type: (string :tag String)

Default: %s

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


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: nil

Enable Proc macro support; implies lsp-rust-analyzer-cargo-run-build-scripts


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-server-command#

Type: (repeat string)

Default: (rust-analyzer)

Command to start rust-analyzer.


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

Type: boolean

Default: nil

Show inlay hints.


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-build-bin#

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

Default: nil

If non-nil, checks the project as if you passed -- bin <build_bin> argument to cargo.

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


lsp-rust-build-command#

Type: (choice (const :tag None nil) (string :tag 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-build-lib#

Type: boolean

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-on-save#

Type: boolean

Default: nil

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


lsp-rust-cfg-test#

Type: boolean

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-clear-env-rust-log#

Type: boolean

Default: t

Clear the RUST_LOG environment variable before running rustc or cargo.


lsp-rust-clippy-preference#

Type: (choice (const on) (const opt-in) (const off))

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-crate-blacklist#

Type: lsp-string-vector

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-features#

Type: lsp-string-vector

Default: []

List of Cargo features to enable.


lsp-rust-full-docs#

Type: boolean

Default: nil

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


lsp-rust-jobs#

Type: (choice (const :tag Auto nil) (number :tag Jobs))

Default: nil

Number of Cargo jobs to be run in parallel.


lsp-rust-library-directories#

Type: (repeat string)

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

List of directories which will be considered to be libraries.


lsp-rust-no-default-features#

Type: boolean

Default: nil

Do not enable default Cargo features.


lsp-rust-racer-completion#

Type: boolean

Default: t

Enables code completion using racer.


lsp-rust-rls-server-command#

Type: (repeat string)

Default: (rls)

Command to start RLS.


lsp-rust-rustflags#

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

Default: nil

Flags added to RUSTFLAGS.


lsp-rust-rustfmt-path#

Type: (choice (const :tag Bundled nil) (string :tag Path))

Default: nil

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


lsp-rust-server#

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

Default: rust-analyzer

Choose LSP server.


lsp-rust-show-hover-context#

Type: boolean

Default: t

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


lsp-rust-show-warnings#

Type: boolean

Default: t

Show warnings.


lsp-rust-sysroot#

Type: (choice (const :tag None nil) (string :tag 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#

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

Default: nil

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


lsp-rust-target-dir#

Type: (choice (const :tag Default nil) (string :tag Directory))

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-unstable-features#

Type: boolean

Default: nil

Enable unstable features.


lsp-rust-wait-to-build#

Type: (choice (const :tag Auto nil) (number :tag Time))

Default: nil

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



Last update: May 17, 2021