rustfmt
rustfmt (via cargo fmt) enforces consistent Rust formatting so reviews focus on behavior, not brace placement.
Busca en todas las páginas de la documentación
rustfmt (via cargo fmt) enforces consistent Rust formatting so reviews focus on behavior, not brace placement.
cargo fmt
cargo fmt --check# rustfmt.toml
edition = "2024"
max_width = 100
imports_granularity = "Module"When to reach for this: Every Rust project; run on save locally and --check in CI.
# rustfmt.toml at repo root
edition = "2024"
max_width = 100
newline_style = "Unix"
use_small_heuristics = "Max"- run: cargo fmt --all -- --checkWhat this demonstrates:
edition aligns formatter with language edition--check fails CI on drift without writing filesrustfmt.toml applies workspace-wide when at rootcargo fmt --all includes workspace members| Option | Effect |
|---|---|
max_width | Line wrap column |
chain_width | Method chain breaking |
format_code_in_doc_comments | Format doctests |
group_imports | Merge/use import style |
rust-analyzer.rustfmt.extraArgscargo fmt --checkrust-toolchain.toml to 1.97.0.rustfmt.toml ignore paths or do not commit generated sources.#[rustfmt::skip] sparingly with comment why.--check on main.edition = "2024" in rustfmt.toml matches Cargo.toml.| Alternative | Use When | Don't Use When |
|---|---|---|
| Manual style guide | Never for Rust teams | Always - use rustfmt |
dprint with rust plugin | Polyglot repos | Pure Rust workspace |
#[rustfmt::skip] | FFI layout-critical blocks | Entire modules |
Repo root for workspaces; applies to all members unless overridden per crate.
fmt is style only; clippy is lint logic. Run both in CI.
Yes - cargo fmt --all includes tests and benches.
ignore = ["src/ffi/generated.rs"] in rustfmt.toml.
No - only whitespace and import order; still run tests after large reformats.
Ships with Rust 1.97 toolchain; no separate install.
Set newline_style = "Unix" for consistent line endings in snapshots and fmt.
pre-commit hook or editor format-on-save; CI --check as backstop.
Nested rustfmt.toml in member crate overrides root for that crate only.
Re-run cargo fmt after resolving; avoid hand-aligning braces.
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 16 jul 2026