
Understand it
Rust documentation examples in Rust code fences can compile and run as tests during cargo test, unless their fence attributes opt out or change execution. A small realistic example teaches usage and fails when an API change makes the documentation stale.
A documentation example is a small public usage test. A Rust code block in a /// comment can be compiled and run by rustdoc during cargo test. The sample demonstrates add_one with an assertion, so a changed return value can fail the documentation check. In a library crate named demo, the example imports the public item as demo::add_one; use the actual crate name in a real package. Documentation that calls an item without bringing it into scope can fail even when the function itself is correct.
Keep a doc example focused on the call a user is likely to make, including setup only when needed. Explain the purpose, important input constraints, and meaning of the return value in prose. You can hide incidental setup lines with rustdoc's # marker, but the behavior being promised should remain visible. Some fences are marked no_run or ignore for specific reasons; those attributes affect whether a block executes, so avoid claiming every fenced block is automatically a test.
Run cargo test --doc after changing a public API and see whether the example still works. Try deliberately changing the asserted value to observe the failure. Then add an edge-case example if it clarifies behavior, but resist turning documentation into a long test suite. A good example is both readable on the docs page and executable in the test harness. It prevents a common drift: code changes while the published instructions continue teaching an obsolete call.
If a code block requires network access or another external resource, it may be unsuitable as a routine doctest. Show the API in a short self-contained example and put slower environment-dependent checks in integration tests. Keep the documentation honest about what runs automatically.
Read the code
/// Adds one.
///
/// ```
/// assert_eq!(demo::add_one(4), 5);
/// ```
pub fn add_one(v: i32) -> i32 { v + 1 }The assert_eq! in the doc comment is compiled and run by cargo test.
Practice starter
/// Adds one.
///
/// ```
/// assert_eq!(demo::add_one(4), 6);
/// ```
pub fn add_one(v: i32) -> i32 { v + 1 }Try it
This add_one example expects 6. Run cargo test --doc and explain the failure.
What success looks like: You can identify a failing doc test, repair its assertion, and explain when no_run or ignore changes execution.
Check your understanding
What checks a Rust doc example?
Reveal the answer
- cargo test
- cargo clean
- rustfmt only
cargo test runs eligible documentation tests.
Go deeper
Common misconception: “A Rust code fence in a /// comment is only documentation, so nothing checks it.”
By default, cargo test compiles and runs Rust code fences in library documentation; no_run and ignore change that behavior.
- Find the documented call: Locate
add_oneand the Rust code fence in its///comment. - Predict the check: Compare
add_one(4)with the asserted value before runningcargo test --doc. - Test an exception: Explain how
no_runandignorewould change what rustdoc checks.
Transfer challenge: Add a second example for add_one(0). Predict whether the doc test passes, then run cargo test --doc.
Connections
- Error Context: Preserve a cause while explaining the failed operation
- Release Profiles: Tune optimization without changing source behavior
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: Executable Documentation (courseId "projects", lessonId "doc-examples") Link: https://crabrix.com/learn/projects/doc-examples/ Goal: By default, cargo test compiles and runs Rust code fences in library documentation; no_run and ignore change that behavior. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=projects/doc-examples) 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.