2

Some IDEs can help with type visualization. Here is an example from VS Code (a slightly modified example from the The Rust Programming Language book):

enter image description here

But other viewers (for instance, used in PR reviews) don't.

Is it possible to setup Rustfmt in such a way that it would always make types explicit? If not - is there an alternative?

PS: I understand that refactoring will be more challenging. But I still prefer to optimize for readability in all code viewers.

ZakiMa
  • 5,637
  • 1
  • 24
  • 48

1 Answers1

5

Is it possible to setup Rustfmt

No. Rustfmt is an AST formatter, it does not do any sort of type analysis, and thus does not do the type inference necessary to insert explicit types.

That would be a rust-analyzer feature, specifically add_explicit_type. Although I don't know whether (and rather doubt that) you can ask RA to bulk-annotate a file. Might be possible to create an LSP client for that specific purpose tho.

But I still prefer to optimize for readability in all code viewers.

I don't think explicitly typing everything does that, personally. Quite the opposite, really.

Masklinn
  • 34,759
  • 3
  • 38
  • 57
  • Thank you! Got it about Rustfmt being an AST formatter. Makes sense. Re: RA. I guess this is what VS Code uses to infer a type. But there is no bulk option. – ZakiMa Jan 23 '23 at 07:07
  • Re: readability. Didn't refer to reading through the code but rather to understanding what the code does. Lack of types require more senior and experienced people to be productive on a team. Having types lowers this bar. I might be wrong in case of Rust though. I'm just starting :) – ZakiMa Jan 23 '23 at 07:08