Formatting linting and rustdoc
Quick answer
Rustfmt formats source, Clippy reports many suspicious or unidiomatic patterns, and rustdoc builds documentation. These tools address different quality concerns; none replaces tests or review.
Tools and documentation comments
/// documents the following item; //! documents the enclosing module or crate. With the optional rustfmt and Clippy components installed, Cargo exposes cargo fmt and cargo clippy. cargo doc --no-deps produces local documentation for the package without generating dependency docs.
/// Returns the price for a number of items costing 3 units each.fn price(count: u32) -> u64 { u64::from(count) * 3 }fn main() { println!("{}", price(4)); }Expected stdout (not executed):
12The comment states what the function promises.
cargo fmt -- --checkcargo clippy --offline --all-targetscargo doc --offline --no-depsManual terminal step; not executed. Expected behavior (environment dependent):
Formatting, lint and documentation checks; results depend on project.Run in a reviewed local package with tools installed. Inspect warnings rather than suppressing them blindly.
Common mistake
A clean lint report is not proof of correct business logic.
Try it yourself
Document a function's input, output and error condition, then inspect its generated docs.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Formatting linting and rustdoc — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
You want documentation for the enclosing crate, not the next function. Which comment form?
Formatting linting and rustdoc — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust /// Returns the price for a number of items costing 3 units each. fn price(count: u32) -> u64 { u64::from(count) * 3 } fn main() { println!("{}", price(4)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Formatting linting and rustdoc — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```bash cargo fmt -- --check cargo clippy --offline --all-targets cargo doc --offline --no-deps ``` Which outcome or interpretation best matches this example?