Search code examples
rustrustfmt

Does Rustfmt have an option to make types explicit?


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.


Solution

  • 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.