Buffer Yard
Language setup

Emacs Rust Setup: rust-analyzer, Cargo and Clippy

Updated

A Rust buffer with a red squiggle under foo.len() and a message that reads like a short essay about borrowing: that is what a working setup looks like, and getting there takes fewer lines of Emacs Lisp than most languages. Nearly all the intelligence lives in rust-analyzer. Emacs only has to start it, show what it says and run Cargo.

Install rust-analyzer the boring way

If you installed Rust through rustup, the language server is one command away:

rustup component add rust-analyzer rustfmt clippy
rustup which rust-analyzer

The second line prints the path to the binary. rustup also places a proxy in ~/.cargo/bin, and that directory has to be on Emacs’s exec-path. If you launch Emacs from a desktop menu and it cannot find rust-analyzer, that missing directory is the reason in almost every case; the install guide covers the PATH problem for each platform.

Installing rust-analyzer through rustup ties its version to your toolchain. That is a good thing: a server much older or newer than the compiler occasionally misreads new syntax.

rust-ts-mode or rust-mode

Emacs 29.1 added rust-ts-mode, built on tree-sitter. It needs the Rust grammar, installed once with M-x treesit-install-language-grammar. It highlights well and indents correctly, and it is all you need if Eglot does the rest.

rust-mode is the older package maintained under the rust-lang organisation on GitHub. It is available from MELPA and NonGNU ELPA, and its value today is less the highlighting than its commands: rust-compile, rust-check, rust-test, rust-run, rust-run-clippy and rust-format-buffer are thin, reliable wrappers around Cargo and rustfmt. Recent versions can sit on top of rust-ts-mode (the option is rust-mode-treesitter-derive), giving you tree-sitter highlighting with rust-mode’s commands.

The better default for a new config is plain rust-ts-mode and running Cargo through M-x compile. Add rust-mode only if you find yourself wanting its commands.

The config

(add-to-list 'major-mode-remap-alist '(rust-mode . rust-ts-mode))
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-ts-mode))

(use-package eglot
  :hook (rust-ts-mode . eglot-ensure)
  :config
  (setq-default eglot-workspace-configuration
                '(:rust-analyzer (:check (:command "clippy")))))

(add-hook 'rust-ts-mode-hook
          (lambda ()
            (add-hook 'before-save-hook #'eglot-format-buffer nil t)))

Eglot already maps Rust modes to rust-analyzer, so there is no eglot-server-programs entry to write. The workspace configuration is the one Rust-specific line, and it is the most useful line on this page.

If you set eglot-workspace-configuration for other languages too, combine the plists into one value, for example (:rust-analyzer (...) :haskell (...)). Setting it twice in two places means the second one wins and the first language silently loses its options.

Clippy as the checker

By default rust-analyzer runs cargo check after each save and reports compiler errors. Pointing check.command at clippy replaces that with cargo clippy, which reports everything check would plus Clippy’s lints. You lose nothing and gain warnings about needless clones, manual loops that should be iterators and the like.

The cost is time. Clippy is slower than a plain check, and on a large workspace the delay between saving and seeing diagnostics grows. If that bothers you, keep the default and run cargo clippy by hand before committing.

Clippy suggestions often come with a machine-applicable fix. rust-analyzer exposes many of them as code actions, so M-x eglot-code-actions on the warning will apply it.

Cargo from the editor

M-x compile with cargo build gives you a compilation buffer whose error lines link back to the source. After the first run, M-x recompile (or g inside the compilation buffer) repeats it. Set the default per project in .dir-locals.el:

((rust-ts-mode . ((compile-command . "cargo test"))))

Tests work the same way: cargo test in a compilation buffer, failures as links. For a single test, cargo test name_of_test is quicker than any wrapper. If you added rust-mode, its commands do this for you with project-root detection built in.

rust-analyzer also offers runnables (the little “Run” and “Debug” lenses some editors show above main and tests), but Eglot does not display code lenses, so in Emacs the compile buffer is the practical route.

rustfmt

The save hook in the config sends formatting to rust-analyzer, which calls rustfmt and respects any rustfmt.toml in the project. With rust-mode installed you could instead set rust-format-on-save to t, which runs rustfmt directly. Pick one; running both formats each file twice.

What about rustic

rustic began as a fork of rust-mode with more features on top: Cargo popups, its own compilation modes and automatic language server setup. For a while it was the most common recommendation. Its author later marked the original repository as no longer active, and development continued in a community-maintained fork. It still works and some people like its interface, but every feature it adds over the setup above is either a convenience wrapper or something Eglot and M-x compile already do. For a config you want to understand line by line, leave it out.

The C setup uses the same compile and recompile loop with Make and CMake, and the Haskell page shows another strongly typed language where the language server carries most of the weight. General package choices are on the packages guide, completion front ends that display rust-analyzer’s suggestions are on the completion guide, and the rest of the languages are listed on the languages index.

One last thing worth knowing: in a Cargo workspace, open Emacs on a file inside any member crate and rust-analyzer finds the workspace root by itself. You do not need to start it from the top directory.

Questions readers ask

Is rustic still worth installing?

It still works, and its Cargo popups are pleasant. But its author marked the original repository as no longer active and maintenance moved to a community fork, so for a new config rust-mode or rust-ts-mode plus Eglot gives you the core features with fewer moving parts.

How do I make rust-analyzer run Clippy instead of cargo check?

Set the rust-analyzer option check.command to "clippy" through eglot-workspace-configuration. Clippy lints then show up as Eglot diagnostics with the normal compiler errors.

Why is the first Eglot start in a Rust project so slow?

rust-analyzer indexes your crate and every dependency and runs a full check before diagnostics appear. On a project with many dependencies this takes a while the first time; later starts reuse the build cache in target/.