liveBook · Manning

Doc test

As explained in Idiomatic Rust

A doc test is an automatically generated test that compiles and executes an executable code example extracted from Rust documentation.

A doc test automatically compiles and executes an executable Rust example extracted from documentation. Running cargo test discovers these examples as tests, helping keep API documentation working. section 6.11.2

Rust-specific mechanics make documentation examples concise:

  • Omit main(); rustdoc preprocesses the example by wrapping it in fn main() { … } and generating test code. section 6.11.2
  • Use cargo watch -x doc to regenerate documentation while editing. section 6.11.2

Executable linked-list documentation

A crate-level example imports LinkedList, creates an empty list, and appends chicken, ostrich, antelope, axolotl, and okapi. The example becomes a passing Doc-test when cargo test runs.

How Idiomatic Rust covers it

How deep it goes
Explains automatic discovery, rustdoc preprocessing, generated main, and cargo test execution.
Examples
Demonstrates crate-level LinkedList usage, animal appends, and passing generated test output.
What you can do after
Apply it via cargo test to verify that documented Rust usage examples compile and execute.
How it connects
Connects API documentation with rustdoc, integration tests, and cargo test.

Also explained in

Compare how 2 books explain doc test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Idiomatic Rust. The section links point to the parts of the book it draws on.