🎓 A1Learner
🔍

Rust rust-analyzer Workflows

rust-analyzer is the tool that checks Rust code inside an editor as it is typed. It shows types that the code does not write out.

Hints are drawn, not written

Grey text appears after a name and names its type. On this page the grey parts are drawn as comments, so you can see them:

let total/* : i32 */ = prices.iter().sum();
let label/* : &str */ = "apples";

Names at the call site

Parameter hints do the same trick at a call. They name the slot each value fills. The names come from the function, not from your call:

send(/* count: */ 3, /* urgent: */ true);

The same errors, no editor

The checks behind the underlines run in a terminal, too. On a project whose match forgets an arm, the report names the same error rustc names. It is E0004, and it points at the match:

// in your terminal:
// $ rust-analyzer diagnostics .

An assist fills the gap

This match forgets Closed, so the code fails. Put the cursor on the match. The editor offers an assist that fills in the missing arms:

fn describe(door: &Door) -> &'static str {
    match door {
        Door::Open => "come in",
    }
}

The rewrite is plain code

The assist writes the missing arm with a placeholder body. You swap the placeholder for your own words. The finished function compiles:

fn describe(door: &Door) -> &'static str {
    match door {
        Door::Open => "come in",
        Door::Closed => "knocked twice",
    }
}

Run and Debug sit above the code

A Run label floats above main, and above every test. The Debug label next to it starts the debugger from the Using a Debugger lesson:

#[test]
fn it_adds() {
    assert_eq!(1 + 1, 2);
}

Example

enum Door {
    Open,
    Closed,
}

fn describe(door: &Door) -> &'static str {
    match door {
        Door::Open => "come in",
        Door::Closed => "knocked twice",
    }
}

fn main() {
    println!("{}", describe(&Door::Open));
    println!("{}", describe(&Door::Closed));
}

Key points

  • Inlay hints are drawings in the editor.
  • They are not text in your file.
  • Switch them off and your file does not change at all
  • An assist acts only when you pick it.
  • The editor offers.
  • You decide.
  • The rewrite lands in your file, where you can read it
  • A runnable is the small Run label above main and above each test.
  • A Debug label sits next to it.
  • Picking one runs cargo run or cargo test for you
  • rust-analyzer re-checks the project as you type.
  • Its background check is cargo check, so most errors surface before you build
  • The same checks run in a terminal.
  • rust-analyzer diagnostics prints the errors for a whole project, with no editor open
  • The editor is a fast reader.
  • The compiler is the judge.
  • rust-analyzer checks code in the editor while it is typed and offers editor help such as hints, assists, and Run labels.

In this example, describe began as the broken match from this lesson. The fill-arms assist wrote the Closed arm. A placeholder stood where the words go, and we replaced it with knocked twice. main asks about both doors. The answers print in order: come in, then knocked twice. The program is ordinary Rust now. The editor help has left, and only the code remains.

Note

If the editor and cargo check ever disagree, believe cargo check.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

You switch inlay hints off in your editor. What happens to your source file?