Skip to content
rusthacks.dev
Tooling

Rust Analyzer Setup in VS Code Neovim and Zed Editors

Set up rust-analyzer in VS Code, Neovim, Helix or Zed, and tune the settings that matter: check on save with Clippy, proc macro support, workspace loading and memory.

2 min read
Summarize in AI
Flat illustration for the post “Rust Analyzer Setup in VS Code Neovim and Zed Editors”, composed around memory.

rust-analyzer is the part of an editor that turns Rust files into completions, errors and jump-to-definition. The defaults are good, and the problems come from three places: it fights your terminal for the build directory, it checks with plain cargo check when you wanted Clippy, and it runs out of patience on a huge workspace. This post fixes each. It extends the toolchain basics in rustup explained.

rust-analyzer current stable, Rust 1.90.

Install

  • VS Code: install the "rust-analyzer" extension. It downloads the server.
  • Neovim: rustup component add rust-analyzer and configure lspconfig.rust_analyzer.
  • Helix, Zed: both detect rust-analyzer on your PATH; Zed bundles it.

Add rust-src so the standard library is navigable: rustup component add rust-src, or list it in rust-toolchain.toml.

Settings worth setting

In VS Code settings.json (the same keys apply to other editors under rust-analyzer.):

settings.jsonJSON
{
  "rust-analyzer.check.command": "clippy",
  "rust-analyzer.cargo.targetDir": true,
  "rust-analyzer.cargo.features": "all",
  "rust-analyzer.procMacro.enable": true,
  "rust-analyzer.files.excludeDirs": ["target", "node_modules", "frontend"]
}
SettingWhy
check.command = "clippy"lints appear in the editor as you save
cargo.targetDir = trueuses target/rust-analyzer, so editor and terminal builds do not wait on one lock
cargo.features = "all"analyzes every feature combination; set a list for crates with conflicting features
procMacro.enableneeded for serde, thiserror and attribute macros

The targetDir option doubles disk use for the project. The trade is worth it, because an editor check that blocks cargo test for ten seconds is annoying every time.

Check what it sees

Run the command "rust-analyzer: Status" in VS Code. It lists the workspace roots, the cfg flags and whether proc macros loaded. If it says "no workspace", the folder opened does not contain a Cargo.toml at its root; open the folder containing it, or add the path in rust-analyzer.linkedProjects.

Slow or wrong results

The usual causes of slow or wrong results are:

  • Wrong cfg: code under #[cfg(feature = "x")] is greyed out because the feature is off. Enable it in cargo.features.
  • Stale build script output: restart the server after changing build.rs.
  • Two toolchains: the server and the terminal use different compilers. rustup show in the project folder reveals it.
  • Memory growth: exclude directories that contain no Rust.

Inlay hints

Type and parameter hints are on by default and can feel noisy. Turn off the ones you dislike under inlayHints.*. They are rendered from analysis and do not change the file.

Where it differs from the compiler

rust-analyzer has its own type checker and can disagree with rustc on edge cases, particularly with complex trait solving. When the editor shows an error that cargo build does not, trust the compiler. The manual is at rust-analyzer.github.io.

Share

Keep reading

Flat illustration for the post “Incident Response for Rust Services a Short Runbook”, composed around deploy.
DevOps

Incident Response for Rust Services a Short Runbook

A one-page runbook for when a Rust service misbehaves: stabilize first, check recent changes, read the four golden signals, find the failing dependency, and use the Rust-specific evidence: panics, memory, executor stalls.

4 min read
Flat illustration for the post “Debugging Async Backtraces and Task Dumps in Tokio”, composed around async.
Tooling

Debugging Async Backtraces and Task Dumps in Tokio

A hung async service has no useful thread backtrace. Tokio task dumps capture the await stack of every task. Enable tokio_unstable and taskdump, trigger a dump from a signal or endpoint, and read where each task is parked.

4 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.