Start with the idea
/// documents the item that follows and //! documents the enclosing module or crate. In a library crate, code blocks inside doc comments are compiled and executed by cargo test, so an example that drifts out of date fails the test run instead of quietly misleading the next reader.
Read the code
/// Returns the larger of two values.
///
/// # Examples
/// ```
/// use lesson_docs::larger;
/// assert_eq!(larger(2, 7), 7);
/// ```
pub fn larger(a: i32, b: i32) -> i32 {
if a > b { a } else { b }
}In a library crate named lesson_docs, cargo test compiles and runs this example.
Practice starter
/// Adds two numbers.
///
/// ```
/// use lesson_docs::add;
/// assert_eq!(add(2, 2), 5);
/// ```
pub fn add(a: i32, b: i32) -> i32 { a + b }
Work through it
Comments are most useful when they preserve a reason the code alone cannot show. // is suitable for a local decision, such as why a limit exists or why a case is skipped. Repeating the next expression in English makes the reader maintain two copies of the same fact. If the code needs a long explanation of what it does, first see whether clearer names or a smaller function would help.
Documentation comments have a different audience. Place /// immediately before an item to explain its purpose, parameters, return value, and conditions that matter to callers. Use //! for an overview of the containing module or crate. A short example makes the contract concrete: it should demonstrate a representative call and assert the expected result. In a Cargo library crate, cargo test runs valid Rust code blocks in documentation as doctests; a binary-only crate does not run them. A doctest is compiled separately, so it must import the documented public item through the crate's actual name or otherwise define everything it uses. A bare call to a library function without an import can fail even when the function itself is correct.
For the practice snippet, the assertion expects five while the function returns four. Predict the failure before running it, then fix the expectation and add an edge case such as zero. Keep an example short enough that a reader can verify the claim at a glance. If an example needs setup, hide only incidental setup lines with rustdoc's # marker; do not hide the behaviour being taught. Treat doc failures as feedback that either code or its promised behaviour changed.
In a published crate, write the actual crate import in the example so the doctest exercises the public function instead of a copy of its logic. Run the test after renaming an API as well as after changing its result.
A comment should explain a decision the code cannot show, such as why a boundary is excluded or why a particular failure is safe to retry. Restating count += 1 as 'increment count' adds little. Rust doc comments beginning with /// attach documentation to an item, and fenced Rust examples in documentation can be compiled and tested by cargo test in a suitable crate. That lets an API example fail visibly when the API changes.
Write one doc comment for a small public function: what it returns, what happens on invalid input, and a short example using the actual crate path. Run its documentation test. Then deliberately change the function signature and see the example fail until you update it. Use ordinary // comments for local reasoning and reserve /// for the contract readers need at the API boundary. Keep examples small enough that someone can copy them without guessing missing imports.
Try it
Document a public function and include one example.
What success looks like: The example states the behaviour and would fail if the code changed.
Check your understanding
What happens when you run cargo test on that in the lesson_docs library crate?
Reveal the answer
- Nothing runs; doc comments are stripped before tests
- The doc test fails because 2 + 2 is not 5
- It prints a warning but the test suite still passes
Doc examples are compiled and asserted, so a wrong example is a failing test.
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Comments & Docs (courseId "basics", lessonId "comments-docs") Link: https://crabrix.com/learn/basics/comments-docs/ Goal: In a library crate, a doc example is a test, so cargo test catches an example that has drifted. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/comments-docs) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.