C.W.K.
Stream
Lesson 05 of 06 · published

clippy & rustfmt

~10 min · tooling, clippy, rustfmt, lint

Level 0Rust Curious
0 XP0/80 lessons0/19 achievements
0/100 XP to next level100 XP to go0% complete

Two tools shape how Rust code looks and reads: rustfmt formats it, and clippy critiques it. Both ship with the toolchain and both are non-negotiable in professional Rust.

rustfmt: one true format

cargo fmt reformats your code to the community standard. There's essentially no configuration debate — Rust deliberately has one canonical style, so every Rust codebase looks the same and code review never argues about whitespace. Run it on save or before every commit; the formatting question is simply settled.

clippy: the lint army

cargo clippy runs hundreds of lints that go beyond correctness into idiom: a manual loop that should be an iterator, a .clone() you don't need, a match with a simpler form, a comparison that's always true. It's like a senior Rust engineer reviewing every line — and following its advice is one of the fastest ways to learn idiomatic Rust.

fmt settles style; clippy teaches idiom. Run cargo fmt and cargo clippy before every commit. fmt removes all style debate; clippy catches non-idiomatic patterns and often explains the better way. Together they're a free, always-on mentor the whole ecosystem agrees with.

Make them automatic

Wire cargo fmt --check and cargo clippy into CI so no un-formatted or lint-failing code merges. Most editors run rustfmt on save. The goal is that style and idiom are enforced by tooling, not reviewers' attention — which frees review to focus on logic and design, the things humans are actually good at.

Code

The two commands to run before every commit·bash
# Format every file to the canonical style (run before committing)
cargo fmt

# Check formatting without changing files (for CI)
cargo fmt --check

# Lint for idiom and common mistakes
cargo clippy

# Treat clippy warnings as errors in CI
cargo clippy -- -D warnings

External links

Exercise

Write a small program with deliberately non-idiomatic code: a manual index loop instead of an iterator, an unnecessary .clone(), messy formatting. Run cargo clippy and cargo fmt and apply their suggestions. For each clippy lint, read its explanation — what idiom did it just teach you?
Hint
clippy will likely suggest replacing the index loop with .iter().enumerate() and removing the needless clone; fmt fixes the whitespace. Each suggestion comes with a rationale — that's the free idiomatic-Rust tutoring.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.