Doctests
Doctests are code examples in /// doc comments that cargo test compiles and runs. They keep documentation honest and demonstrate API usage inline with rustdoc.
Search across all documentation pages
Doctests are code examples in /// doc comments that cargo test compiles and runs. They keep documentation honest and demonstrate API usage inline with rustdoc.
/// Adds two integers.
///
/// ```
/// let sum = my_crate::add(2, 2);
/// assert_eq!(sum, 4);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
a + b
}When to reach for this: Public API examples that must stay synchronized with code changes.
/// Parses a user id from text.
///
/// # Errors
///
/// Returns `Err` when the input is not a positive integer.
///
/// ```
/// use my_crate::parse_user_id;
///
/// let id = parse_user_id("42")?;
/// assert_eq!(id, 42);
/// # Ok::<(), my_crate::Error>(())
/// ```
pub fn parse_user_id(s: &str) -> Result<u64, Error> {
// ...
todo!()
}cargo test --doc
cargo test --doc parse_user_idWhat this demonstrates:
? in examples needs trailing # Ok::<(), E>(()) hidden linecargo test --doc runs only doctests/// ```no_run
/// let client = Client::connect("postgres://...").await?;
/// ```
///
/// ```ignore
/// // pseudo-code not compiled
/// ```
///
/// ```should_panic
/// panic!("bad");
/// ```| Marker | Behavior |
|---|---|
no_run | Compile but do not run |
ignore | Skip compile (display only) |
should_panic | Expect panic |
edition2024 | Set edition for snippet |
Prefix with # to hide setup from rendered docs:
/// ```
/// # use my_crate::Widget;
/// let w = Widget::default();
/// assert!(w.ready());
/// ```# Ok::<(), E>(()) tail.async doctest unstable features. Fix: use no_run or show sync API; link async guide.# lines.ignore for pseudocode.| Alternative | Use When | Don't Use When |
|---|---|---|
examples/ directory | Runnable multi-file demos | One-liner API proof |
| mdbook | Book-length narrative | Inline API rustdoc |
| Integration tests only | Complex Axum apps | Simple pure functions |
Doctests run on public items by default. Private module docs can have doctests but are not shown in public docs.
Prefer no_run with async block in docs or document with link to integration test. Full async doctests need nightly attributes.
Yes if the crate is a dependency; dev-deps are not available to doctests on the lib.
Doctests document usage; unit tests cover edge cases without cluttering public docs.
Use ```ignore or remove the code block; or #[doc(hidden)] on internal APIs.
cargo test --doc --workspace runs per crate; filter with -p.
Use ```cfg(feature = "foo") on the code block (rustdoc cfg) so examples match feature docs.
Doctests capture output; use unit tests for println! verification or document expected behavior in prose.
Rustdoc wraps snippets in one function; split into multiple code blocks for multiple scenarios.
docs.rs builds docs; doctest execution happens in your CI via cargo test --doc.
cargo testmissing_docsStack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Reviewed by Chris St. John·Last updated Jul 16, 2026