Formatting Rust code with rustfmt and cargo fmt

rustfmt is the official formatter for Rust. It rewrites your source to follow the Rust style guide, so code reviews don’t get bogged down in brace placement and line wrapping. Almost every Rust project runs it, which also means most Rust code you read looks the same.
Installing
Section titled “Installing”rustfmt ships as a rustup component and is included in the default profile, so you probably have it already:
cargo fmt --versionIf that fails, add it:
rustup component add rustfmtRunning it
Section titled “Running it”From the root of a Cargo project (or workspace), format every crate:
cargo fmtTo check formatting without changing files, which is what you want in CI:
cargo fmt --checkIt exits with a non-zero status and prints a diff if anything would change. A minimal GitHub Actions step is just run: cargo fmt --all --check (--all covers every package in a workspace).
You can also run rustfmt on a single file:
rustfmt src/main.rsOne gotcha: cargo fmt passes the crate’s edition from Cargo.toml to rustfmt, but calling rustfmt directly does not, and it defaults to the 2015 edition. Code using newer syntax (such as async fn) can then fail to parse. Pass --edition 2024 (or whichever edition you use), or set edition in rustfmt.toml.
Configuration
Section titled “Configuration”Most projects use the defaults. If you want to change something, put a rustfmt.toml or .rustfmt.toml in the project root. rustfmt looks in the current directory and then its parents, and uses the first file it finds.
edition = "2024"max_width = 100hard_tabs = falsetab_spaces = 4newline_style = "Auto"use_small_heuristics = "Default"The values above are all the defaults except edition, so this file only shows where the knobs are:
edition: the Rust edition used to parse the code ("2015","2018","2021","2024").cargo fmtsets it fromCargo.toml.style_edition: which version of the style guide to format with. Likeedition,cargo fmtinfers it from your crate’s edition, so you rarely set it.max_width: maximum line width. Default100.hard_tabs/tab_spaces: indent with tabs or with this many spaces. Defaultsfalseand4.newline_style:"Auto"(default),"Native","Unix"or"Windows".use_small_heuristics: how aggressively short expressions (function calls, struct literals, chains) are kept on one line."Default","Off"or"Max"."Max"packs as much onto one line asmax_widthallows.
Some popular options are still unstable, for example imports_granularity and group_imports for organizing use statements. They only take effect on the nightly toolchain:
cargo +nightly fmtIf you set unstable options in rustfmt.toml and run stable cargo fmt, rustfmt prints a warning and ignores them.
To leave a specific item alone, mark it with #[rustfmt::skip]. This is useful for hand-aligned tables or matrices.
The full list of options, with which are stable, is in the rustfmt configuration docs.
Editor integration
Section titled “Editor integration”Most editors run rustfmt through rust-analyzer, the Rust language server:
- VS Code: install the rust-analyzer extension and enable
editor.formatOnSave. The olderrust-lang.rustextension is deprecated. - JetBrains: RustRover (or the Rust plugin in other JetBrains IDEs) can use rustfmt instead of its built-in formatter. Turn this on in the Rustfmt settings page.
- Sublime Text: LSP-rust-analyzer or Rust Enhanced.
- Vim/Neovim: rust.vim provides
:RustFmtandlet g:rustfmt_autosave = 1for format-on-save. Neovim users with an LSP setup can format through rust-analyzer instead.
Formatting on save plus cargo fmt --check in CI is enough to keep formatting out of code review.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)