Start with the idea
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 learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.