Sub topic 4
Opting out of formatting for specific items
This chapter assumes you can run cargo fmt and have a project with a rustfmt.toml, as covered in the earlier chapters. It shows how to protect individual pieces of code from rustfmt.
Why you might opt out
rustfmt is deliberately opinionated. For most code, its output is what you want. A few kinds of code read better when a human lays them out:
- Tables of numbers where columns line up.
- Matrices and lookup tables.
- Long literal data that rustfmt would reflow into one value per line.
- Generated code that you don't edit by hand.
For these cases, Rust provides the #[rustfmt::skip] attribute. rustfmt leaves the marked item exactly as you wrote it. Everything else in the file is still formatted.
Skipping a single item
Put the attribute directly above the item. This example keeps a hand-aligned constant:
#[rustfmt::skip]
const IDENTITY: [[u8; 3]; 3] = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
];
fn main() {
println!("{:?}", IDENTITY);
}Without the attribute, rustfmt could collapse the short rows. The table would then lose its visual shape. With the attribute, cargo fmt leaves the constant unchanged.
rustfmt::skip is a tool attribute. The compiler accepts it in normal builds, so you don't need a feature flag or a nightly toolchain.
Skipping statements, functions, and modules
The attribute works on more than constants. You can place it on:
- A
letstatement inside a function. - A whole function.
- An
implblock. - A module declaration.
A statement-level skip is useful when one local value needs custom layout:
fn main() {
#[rustfmt::skip]
let weights = [
1, 2, 4,
8, 16, 32,
];
let total: i32 = weights.iter().sum();
println!("total = {total}");
}Only the weights statement is protected. The let total line below it is still formatted normally.
To skip a whole module, mark its declaration:
#[rustfmt::skip]
mod generated;This protects the module's contents, including a generated.rs file that the declaration loads. It's a good fit for code produced by a tool. That code would otherwise change every time someone runs cargo fmt.
Choosing the smallest scope
The wider the skip, the more code escapes formatting. Reviewers then stop seeing consistent style in that area. Use this order of preference:
- Reformat the code so rustfmt's output is acceptable.
- Skip a single statement or constant.
- Skip a function or
implblock. - Skip a whole module, only for generated code.
Common mistakes
- Putting the attribute in the wrong place. The attribute applies only to the item that follows it. A blank line or a comment between them is fine, but another item in between moves the skip to that item.
- Skipping to hide a style disagreement. If you dislike a rule, change
rustfmt.tomlinstead. A configuration option applies the change everywhere and keeps the code consistent. - Expecting Clippy to ignore the item.
#[rustfmt::skip]affects only formatting. Clippy still lints the item. To silence a lint, use#[allow(clippy::lint_name)], which a later chapter covers. - Forgetting that
--checkrespects the skip.cargo fmt --all -- --checkreports no difference for skipped items, so CI passes. You don't need a separate exclusion in your pipeline.
Exercise
Exercise
You have a function with one let statement holding a hand-aligned array. You want rustfmt to keep that array unchanged but still format the rest of the function. Where do you put #[rustfmt::skip], and does cargo fmt --all -- --check fail for the skipped array?
Show answer
Put #[rustfmt::skip] directly above that let statement, not above the function. The rest of the function is still formatted. The --check command does not fail for the skipped array, because rustfmt treats the statement as already formatted. It fails only if other unskipped code differs from rustfmt's output.