8

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.

Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68
jsstuball
  • 4,104
  • 7
  • 33
  • 63
  • 1
    Does it show when you create a function that has more than one parameter? – Jason Feb 12 '21 at 15:47
  • 1
    Are you sure that language server is installed. Rust-analyzer suggests to install it after being itself installed using a popup which really easy to miss. – Angelicos Phosphoros Feb 12 '21 at 18:58
  • Both comments turned out to be useful! Fwiw I think there was a conflict between rust-analyzer and the VSC rust-lang plugin, causing some features to not work. – jsstuball Feb 13 '21 at 14:51

2 Answers2

6

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.
Jason
  • 4,905
  • 1
  • 30
  • 38
  • Just to check, are you using `matklad.rust-analyzer` or `rust-lang.rust` VSC extension? It's confusing because the latter says "powered by Rust Analyzer" and it seems to have a higher install count. Looks like the extension reviews are better for `matklad.rust-analyzer` – jsstuball Feb 13 '21 at 14:54
  • 1
    @JSStuball it indeed is confusing. I _only_ have [`rust-analyzer`](https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer) installed as an extension. It is part of the `rls-2.0` effort. Perhaps there is a conflict? The `rust-lang.rust` extension mentions that it is able to install either. – Jason Feb 13 '21 at 17:49
  • Alright cheers, everything appears to be working fine with only `rust-analyzer` enabled so I'll roll with it for now. – jsstuball Feb 13 '21 at 23:34
  • 1
    @JSStuball great! I've added some more information to my answer that mentions the possible conflict between these two. – Jason Feb 14 '21 at 00:33
4

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.

Ibraheem Ahmed
  • 11,652
  • 2
  • 48
  • 54