Sub topic 1
Why Format and Lint: Consistency and Fewer Bugs
Prerequisites
This chapter assumes you can do the following:
- Read and write basic Rust, including ownership and borrowing.
- Create and build a project with
cargo newandcargo build. - Use a terminal and a code editor.
You do not need any prior experience with rustfmt or Clippy.
Two tools, two jobs
Rust ships two official tools that improve code without changing what it is meant to do.
rustfmtis a formatter. It rewrites whitespace, line breaks, and layout to match one standard style. It never changes what your program does.- Clippy is a linter. A linter is a tool that reads your code and reports patterns that are likely bugs, needlessly slow, or unidiomatic. Idiomatic code is code written the way experienced Rust programmers usually write it.
| Question | rustfmt | Clippy |
|---|---|---|
| What does it do? | Rewrites layout | Reports problems and suggests fixes |
| Does it change program behavior? | No | Only if you apply a suggested fix |
| Command | `cargo fmt` | `cargo clippy` |
| Typical output | Edited files | Warnings with explanations |
Both tools are distributed as rustup components. The default rustup profile installs them. If yours does not, add them:
rustup component add rustfmt clippy
cargo fmt --version
cargo clippy --versionWhat consistency buys you
Without a formatter, a team spends review time on style: brace placement, line length, where to wrap a long call. These arguments are cheap to start and never end.
With rustfmt, the question has one answer. The tool decides, and everyone gets the same result.
- Smaller diffs. When every editor produces the same layout, a pull request shows only real changes. It does not show someone's whitespace preferences.
- Faster reading. Code from any Rust project looks familiar, so you spend your attention on logic.
- Fewer review comments. Reviewers can focus on design and correctness.
The standard style is the default of rustfmt. Most Rust projects use it, so following it also means your code looks like the code in the libraries you depend on.
What linting buys you
The compiler already catches type errors and borrow errors. Clippy goes further and looks at code that compiles but is probably not what you meant, or is harder to read than necessary.
Here is a small program that compiles without complaint:
fn main() {
let items: Vec<i32> = Vec::new();
if items.len() == 0 {
println!("empty");
}
}Clippy reports the clippy::len_zero lint here. A lint is a single named check, and each one has a name you can search for and configure. This lint suggests items.is_empty(), which states the intent directly and works for every collection type. Many other lints go beyond style. Some flag logic that is almost certainly wrong, such as comparing a value with itself or a comparison that can never be true.
Each Clippy diagnostic names the lint, points at the code, and usually shows the suggested replacement. Reading these messages is also a way to learn idiomatic Rust.
How the tools fit into your workflow
The tools work best when they run automatically at every stage, not occasionally by hand. The diagram shows the usual order.
Formatting runs first because it is fast and removes noise. Clippy runs next, so you fix real findings in already formatted code. Tests come last. CI repeats the same checks so that nobody can merge code that skipped them locally.
Common mistakes to avoid
- Treating the formatter as optional. If only some contributors run
rustfmt, every commit from the others reformats files and the diffs grow noisy again. Agree on one rule, and enforce it in CI. - Applying every Clippy suggestion blindly. Read the message first. A suggestion that compiles is not always clearer in your context.
- Disabling lints crate-wide to make warnings disappear. This hides future problems along with the current ones. Prefer a narrow exception.
The following chapters cover each tool in turn: running and configuring rustfmt, then Clippy's lint groups, levels, and automatic fixes, and finally CI and editor integration.
Exercise
A teammate says, "We don't need Clippy because the compiler already checks our code, and we don't need rustfmt because we review style by eye." Give one thing each tool provides that this plan misses.
Show answer
Clippy reports code that compiles but is probably wrong or unidiomatic, such as items.len() == 0 where items.is_empty() is clearer (clippy::len_zero). The compiler does not report this. rustfmt applies one consistent layout automatically, which removes style discussion from reviews and keeps diffs limited to real changes. Manual review is slow and inconsistent.