Quickstart: Search VSCode extension:
Prettier - Code formatter (Rust) [link]
Visit the repository to see other install options (Core extension, as a plugin, in the CLI, as a crate...)
What usually happens once people start using Prettier is that they realize how much time and mental energy they actually spend formatting their code. No matter how incomplete or broken the code you're working on is, with the Prettier Editor Extension you can always just press the
Format Documentkey binding and *poof*, the code snaps right into place.
- Beautiful, uniform and consistent — Prettier is strongly opinionated, with no style options.
- There when you need it the most — Prettier can format code that won't compile (e.g. missing annotations)
- Speed up the day-to-day — Prettier auto-fixes common syntax errors (e.g. missing semicolons, blocks, parentheses)
|> input||> formatted|
Formatting succeeds and fixes 7 syntax errors.
Complete config with default values:
# .prettierrc.toml useTabs = false tabWidth = 4 printWidth = 100 endOfLine = "lf" # Not supported yet # trailingComma = "es5" # embeddedLanguageFormatting = "auto"
Unfortunately Rustfmt cannot implement those features by design.
Rustfmt parses code with rustc. Rustc is strict and unforgiving as it always assumes code is at its "final version", thus every slight deviation from the accepted syntax crashes the parser. There's also that rustc has many lint-like checks within the parser. The intention is to save work for the compiler down the line, unfortunately it also means that rustc sometimes fails to parse syntactically correct code.
Jinx-rust has a little plaidoyer in its readme arguing for Rust Tooling not to use the official rustc parser here.
You can effectively think of Prettier Rust syntax autocorrection as auto-applying the Rust compiler's suggested syntax fixes automatically (e.g. "semicolon missing here", "parenthesize this" or "add a block around that")
Otherwise if your codebase compiles, then Prettier Rust is just a formatter like any other. It won't change the syntax of valid rust code. Moreover, it doesn't reorganize imports, split comments or combine attributes.
Rest assured, Prettier Rust actually does not take style decisions on its own. Prettier Rust is essentially a 1:1 adaptation of Prettier Typescript, hence the opinions it implements have been battle tested and agreed-upon by millions and millions of users already.