Prettier Rust is an opinionated code formatter that autocorrects bad syntax.
MIT License
The massively popular Prettier code formatter, now with Rust support!
Get Started: Install VSCode Extension Prettier - Code formatter (Rust)
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 Document
key binding and *poof*, the code snaps right into place.
const LEET = 1337
/// My WIP code draft
#![feature(crate_visibility_modifier)]
async crate fn foo(arg) {
arg.0 *= 3.14 + LEET & 1337
arg.1(|b, c| -> T &c).await
}
const LEET = 1337;
#![feature(crate_visibility_modifier)]
/// My WIP code draft
crate async fn foo(arg) {
arg.0 *= (3.14 + LEET) & 1337;
(arg.1)(|b, c| -> T { &c }).await
}
Formatting succeeds and fixes 7 syntax errors.
https://prettier.io/docs/en/configuration
// .prettierrc.json
{
"useTabs": false,
"tabWidth": 4,
"printWidth": 100,
"endOfLine": "lf",
// -- Not supported yet --
// "trailingComma": "es5",
// "embeddedLanguageFormatting": "auto",
// Example override
"overrides": { "files": ["tests/*.rs"], "options": { "printWidth": 80 } }
}
# .prettierrc.toml
useTabs = false
tabWidth = 4
printWidth = 100
endOfLine = "lf"
# -- Not supported yet --
# trailingComma = "es5"
# embeddedLanguageFormatting = "auto"
# Example override
overrides = [
{ files = ["tests/*.rs"], options = { printWidth = 80 } }
]
// prettier-ignore
or #[rustfmt::skip]
above it#![rustfmt::skip]
inside blocks or files.prettierignore
file to glob-match files, like .gitignore
!{}
format like blocks, ![]
and !()
like comma-separated expressionsmatches!
, if_chains!
...) [Not implemented yet]
Yes! Prettier Rust formats most nightly features. Support depends on jinx-rust
.
Recommended
Extension StandaloneEasy install + auto-updates
VSCode | Search and install Prettier - Code formatter (Rust)
[direct link]
Request your favorite editor: [file an issue]
Alternative
Core Extension PluginRequires NodeJS + Prettier Extension (built-in Jetbrains IDEs)
npm install --global prettier-plugin-rust prettier
Restart IDE after installing.
To update (manual only!!): npm upgrade --global prettier-plugin-rust prettier
To check installed version: npm ls -g --depth=0 prettier-plugin-rust prettier
To check latest version: npm info prettier-plugin-rust version
Requires NodeJS
Install prettier
and prettier-plugin-rust
globally
npm install --global prettier-plugin-rust prettier
Use the prettier CLI to format rust files. E.g. run:
prettier --write **/*.rs
Requires NodeJS
Install prettier
and prettier-plugin-rust
in the project
npm install --save-dev prettier-plugin-rust prettier
Link to the plugin's location in your prettier config:
"plugins": ["./node_modules/prettier-plugin-rust"]
Use the prettier CLI to format rust files. E.g. run:
npx prettier --write **/*.rs
You can also use the plugin programmatically:
import prettier from "prettier";
import * as rustPlugin from "prettier-plugin-rust";
prettier.format(code, { plugins: [rustPlugin] });
No crate yet. Above options are available in the meantime.
cargo fmt
?
It's all about the Editor Integration — Having the ability to format your code while you work on it really makes for a great developer experience, and autocompletion for Rust's strict syntax is such a massive time save. Once you've tried the extension there really is no coming back.
All-in-all the difference in code style is minimal, so adopting Prettier Rust won't drastically change your codebase. The real downside is the harsher integration with the Rust ecosystem, but it'll get better eventually.
Point by point:
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.
Prettier Rust however is based on jinx-rust. Jinx-rust is built specifically for Rust tooling. Hence it's designed to tolerate a wide range of syntax errors, supports missing nodes and sometimes even infers user intent (e.g. Javascript's !==
)
Jinx-rust has a little plaidoyer in its readme arguing for Rust Tooling not to use the official rustc parser here.
The Prettier Rust syntax autocorrection feature is intended to be an adaptation of how Prettier Typescript autocorrects javascript code with missing semicolons.
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.