Expert · Testing, tooling and unsafe Rust · topic 41 of 50 · 25 min
Tooling: rustfmt, Clippy, docs and editions
Explanation
Why tools matter
Professional Rust teams rarely argue about style or catch simple mistakes in code review, because tools do it automatically. Rust ships a standard set of tools that every project uses the same way, so you can join any Rust codebase and feel at home.
rustfmt: one style for everyone
cargo fmt # format the whole project
cargo fmt --check # only check; fails if anything is not formatted (for CI)Everyone uses the same default style, so code from any project looks familiar. Set your editor to format on save. You can adjust a few options in a rustfmt.toml file, but most teams keep the defaults.
Clippy: a friendly expert reviewer
Clippy is a linter: a tool that looks for code that compiles but is likely wrong, slow or not idiomatic (not in the usual Rust style). It has hundreds of checks, called lints, each with an explanation and usually a suggested fix.
cargo clippy
cargo clippy -- -D warnings # treat every warning as an error (for CI)warning: this `if` has identical blocks
warning: using `clone` on type `u32` which implements the `Copy` trait
warning: length comparison to zero
help: using `is_empty` is clearer and more explicit: `v.is_empty()`Other cargo tools
| Command | Use |
|---|---|
| cargo check | Type-check without building a program; fastest feedback loop |
| cargo doc --open | Build docs for your crate and all dependencies, offline |
| cargo tree | Show the dependency tree, to spot duplicates and heavy crates |
| cargo update | Update dependencies within the versions Cargo.toml allows |
| cargo build --release | Optimised build for production |
| cargo install <tool> | Install a Rust command-line tool from crates.io |
Security-minded teams also use community tools such as cargo-audit, which checks Cargo.lock against the RustSec database of known vulnerable crate versions. Install it with cargo install cargo-audit and run cargo audit.
Profiles
[profile.release]
opt-level = 3 # the default for release
lto = true # link-time optimisation: smaller, faster, slower to build
codegen-units = 1 # better optimisation, slower buildEditions
An edition is a set of language rules chosen per crate in Cargo.toml (edition = "2024"). Editions let Rust fix old design choices without breaking existing code: crates on different editions still work together in one program. Editions so far: 2015, 2018, 2021 and 2024 (released with Rust 1.85 in February 2025).
cargo fix --edition # apply automatic fixes before switching
# then change edition = "2021" to edition = "2024" in Cargo.toml
cargo buildA simple CI routine
CI (continuous integration) means a server runs your checks automatically on every push or pull request. A typical Rust CI job runs four commands; if any fails, the change is not merged.
cargo fmt --check
cargo clippy --all-targets -- -D warnings
cargo test
cargo build --releaseCommon mistakes
- Ignoring Clippy warnings for months until there are hundreds. Fix them as they appear.
- Silencing a lint with #[allow(...)] without a comment explaining why.
- Not committing Cargo.lock for applications, so CI builds different versions than your laptop.
- Measuring speed with cargo run (debug). Use --release.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Tooling: rustfmt, Clippy, docs and editions · 1/6
Rust ships standard tools, so every project is formatted and checked the same way.
Prefer a video by a teacher? Find free videos on “rust clippy rustfmt cargo tools ci github actions” on YouTube ↗
In real life: Keeping a team codebase healthy
A five-person team at a logistics startup builds a route-planning service in Rust. Their GitHub Actions workflow runs cargo fmt --check, cargo clippy -- -D warnings and cargo test on every pull request. Reviews focus on design and business logic, because formatting and common mistakes never reach review. When Rust 2024 arrived, cargo fix --edition handled most of the upgrade in an afternoon.
// Before Clippy:
fn has_orders(orders: &Vec<u32>) -> bool {
if orders.len() == 0 { false } else { true }
}
// After following Clippy's suggestions:
fn has_orders_clean(orders: &[u32]) -> bool {
!orders.is_empty()
}Practice (0/3 done)
Which command fails in CI if code is not formatted, without changing files?
What is an edition in Rust?
In one of your projects, run cargo fmt, cargo clippy, cargo doc --open and cargo tree. Fix every Clippy warning (write down two you learnt from). Then add a GitHub Actions workflow (or a simple script) that runs fmt --check, clippy -D warnings and test.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Unsafe Rust: what it is and is notTesting, tooling and unsafe Rust · same module
- Unit testsTesting, tooling and unsafe Rust · same module
- Installing Rust with rustupGetting started with Rust
- Installing Go and the go commandGo Programming (backend and cloud) · Getting started with Go
- Portfolio, open source and a learning planRust careers and interviews
- CI/CD: shipping every change safelyFull-Stack Development · Deploying and running the app
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What is Clippy?
Answer: A linter that suggests fixes for likely bugs and non-idiomatic code
Clippy adds hundreds of lints on top of the compiler.
Which is the fastest way to see compile errors while coding?
Answer: cargo check
check skips generating the final program.
Which edition did Rust 1.85 introduce?
Answer: 2024
The 2024 edition became stable with Rust 1.85 in February 2025.
What does cargo-audit check?
Answer: Dependencies against a database of known vulnerabilities
It uses the RustSec advisory database.
What is Cargo.toml?Related
Answer: The project settings: name, version, edition and dependencies
Cargo.toml describes the project and lists the crates it depends on.
What happens to code blocks inside /// comments when you run cargo test?Related
Answer: They are compiled and run as tests
Doc tests keep examples correct.
Why commit Cargo.lock for an application?Related
Answer: It pins exact dependency versions so builds are repeatable
Cargo.lock records the exact versions used.
What does rust-analyzer give you?Related
Answer: Smart editor features: errors, types and completion as you type
It is a language server that powers editor features for Rust.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 1 more topic in “Testing, tooling and unsafe Rust” before its exam: Unsafe Rust: what it is and is not.
- Module 10 exam: Testing, tooling and unsafe Rust · 12 questions · 25 min, without help. 4 topics of this module still to finish first.
- Then the next module, Real projects: CLI, web and WebAssembly. 50 topics left in the course before the final exam.