Skip to content

Expert · Testing, tooling and unsafe Rust · topic 41 of 50 · 25 min

Tooling: rustfmt, Clippy, docs and editions

Explanation

cargo fmt formats code in the standard style; cargo clippy points out bugs and non-idiomatic code; cargo doc builds documentation; cargo check is the fastest way to find errors. Editions (2015, 2018, 2021, 2024) let Rust improve without breaking old code, and cargo fix --edition helps you upgrade. Run fmt, clippy and test on every push.

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

bash
cargo fmt            # format the whole project
cargo fmt --check    # only check; fails if anything is not formatted (for CI)
Formats indentation, spacing, line breaks and more.

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.

bash
cargo clippy
cargo clippy -- -D warnings   # treat every warning as an error (for CI)
Run it often; it teaches you idiomatic Rust.
output
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()`
Typical Clippy messages: each one links to a page explaining the lint.

Other cargo tools

CommandUse
cargo checkType-check without building a program; fastest feedback loop
cargo doc --openBuild docs for your crate and all dependencies, offline
cargo treeShow the dependency tree, to spot duplicates and heavy crates
cargo updateUpdate dependencies within the versions Cargo.toml allows
cargo build --releaseOptimised 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

ini
[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 build
Profiles in Cargo.toml tune debug and release builds. Defaults are good; change them when you measure a need.

Editions

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).

bash
cargo fix --edition     # apply automatic fixes before switching
# then change edition = "2021" to edition = "2024" in Cargo.toml
cargo build
The Edition Guide lists what changed in each edition.

A 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.

bash
cargo fmt --check
cargo clippy --all-targets -- -D warnings
cargo test
cargo build --release
The same four commands are worth running locally before you push.

Common 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

fmt | clippy | doc | check

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.

rust
// 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()
}
Clippy suggests &[u32] instead of &Vec<u32>, is_empty() instead of len() == 0, and removing the needless if.

Practice (0/3 done)

Task 1 · Question

Which command fails in CI if code is not formatted, without changing files?

Task 2 · Question

What is an edition in Rust?

Task 3 · Practice

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

Question 1

1.What is Clippy?

Question 2

2.Which is the fastest way to see compile errors while coding?

Question 3

3.Which edition did Rust 1.85 introduce?

Question 4

4.What does cargo-audit check?

Read more (free)

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.

From Your first program and cargo

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.

From Integration tests and doc tests

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.

From Packages, crates and modules

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.

From Installing Rust with rustup

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. 1 more topic in “Testing, tooling and unsafe Rust” before its exam: Unsafe Rust: what it is and is not.
  2. Module 10 exam: Testing, tooling and unsafe Rust · 12 questions · 25 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Real projects: CLI, web and WebAssembly. 50 topics left in the course before the final exam.

← Previous: Integration tests and doc tests