How to get type hints to display?

17.5k Views Asked by At

I have seen youtubers and such working on Rust in VSC with rust-analyzer plug-in where they get the optional type annotations displayed, even if it isn't necessarily written in the code. It's like I type foo(a,b) in the editor and it automagically displays foo(a: A, b: B) where the :A and :B are in faint grey possibly not even written in the file, just visual hint? It's nice and I can't figure out whether this is a feature of VSC or rust-analyzer? My rust-analyzer has the two settings Parameter Hints and TypeHints both set to enabled.

2

There are 2 best solutions below

4
On BEST ANSWER

You're looking for parameter hints in this case. The function for which you want to display hints also needs to have more than one parameter.

Make sure that the setting is enabled:

Settings (UI)

Inlay hints: Parameter hints

Settings (JSON)

"rust-analyzer.inlayHints.parameterHints": true

You should then end up with something akin to the following:

fn add(x: u32, y: u32) -> u32 {
    x + y    
}

fn main() {
    let result = add(x: 4, y: 2);
}

Make sure that only rust-analyzer is enabled as it can conflict with rls. A warning was added which mentions the following if both are enabled:

You have both rust-analyzer (matklad.rust-analyzer) and Rust (rust-lang.rust)
plugins enabled. These are known to conflict and cause various functions of
both plugins to not work correctly. You should disable one of them.
0
On

rust-analyzer shows inlay hints for:

  • Types of local variables
  • Names of function arguments
  • Types of chained expressions

You can toggle inlay hints by adding this to your settings.json:

{
  "rust-analyzer.inlayHints.enable": true
}

Or you can search "rust inlay" in your VSCode preferences.