3

When I type this in VSCode:

let mut guess = String::new();

I see it changed to:

let mut guess: String = String::new();

Why is this happening?

kmdreko
  • 42,554
  • 6
  • 57
  • 106
  • The latter is explicitly specifying the type, but it's not strictly required. VSCode is probably just being pedantic. – ShadowRanger Jun 15 '22 at 01:27
  • See also [How can I remove type annotation help when using rust-analyzer?](/q/69909997/2189130) – kmdreko Oct 17 '22 at 17:05

1 Answers1

7

These are called inlay hints. rust-analyzer shows you the inferred type of the variable so you know it, for easier code reading. VSCode does not actually change the code, just how it displays it.

If you don't like it, you can turn it off by setting rust-analyzer.inlayHints.typeHints.enable to false. This disables only this kind of inlay hints; there are many others you can control via the settings from the group rust-analyzer.inlayHints.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
  • There's also an action to disable it - CTRL+SHIFT+P (by default) and type "toggle inlay hints." Much easier manually tweaking settings. – cdhowie Jun 15 '22 at 03:45
  • Although I don't recommend disabling them, they are incredibly helpful once you get used to them. Whenever I use an editor that doesn't have them (like https://play.rust-lang.org/) I keep making silly mistakes and programming is much slower than with hints. – Finomnis Jun 15 '22 at 09:33
  • @Finomnis One nice trick is to explicitly type variables as `()` and then the compiler tells you the real type in the error message. – Chayim Friedman Jun 15 '22 at 10:16
  • @ChayimFriedman Yah that's what I usually do, but it's a lot more tedious than mouseover or inline hints – Finomnis Jun 15 '22 at 10:18