Sub topic 8
Hiding setup lines with #
This chapter assumes you can write /// comments and run cargo test --doc. It also assumes you know what the ? operator does.
Why hide lines
A doc example has to compile as a complete program. That usually means imports, helper values, and sometimes a return value. Readers of the rendered page only need the lines that show how the API is used. Rustdoc lets you keep the extra lines in the test and remove them from the HTML.
The # prefix
Start a line in a doc example with # (a hash and a space). Rustdoc compiles and runs the line as part of the test but leaves it out of the rendered documentation.
use std::num::ParseIntError;
/// Parses a decimal port number.
///
/// # Errors
///
/// Returns [`ParseIntError`] if `s` is not a valid `u16`.
///
/// # Examples
///
/// ```
/// # use mycrate::parse_port;
/// let port = parse_port("8080")?;
/// assert_eq!(port, 8080);
/// # Ok::<(), std::num::ParseIntError>(())
/// ```
pub fn parse_port(s: &str) -> Result<u16, ParseIntError> {
s.trim().parse()
}The rendered page shows only two lines:
let port = parse_port("8080")?;
assert_eq!(port, 8080);The hidden use line makes the test compile. The hidden last line makes the ? legal, as the "Using ? and Result" chapter explains.
What the prefix does
| You write | Rustdoc compiles it | Readers see it |
|---|---|---|
| `# let x = 1;` | Yes | No |
| `let x = 1;` | Yes | Yes |
| `## comment` | Yes, as `# comment` | Yes, as `# comment` |
The third row is the escape. If the visible code must contain a line that starts with #, such as an attribute-like line or a shell comment in a non-Rust block, write ##. Rustdoc removes one # and shows the rest.
A bare # on a line by itself hides that line, which is useful for blank lines.
Good and bad candidates for hiding
Hide lines that are plumbing:
usestatements for the item being documented.- Setup that the reader does not need to learn from, such as creating a temporary value.
- A closing
Ok::<(), E>(())expression.
Do not hide lines that explain the API. If the example only works because of a hidden let mut list = List::new();, a reader who copies the visible lines gets a compile error. Hidden code is invisible, so treat it as a promise that the visible code is enough to understand.
Checking what you hid
Build the docs and read the page the way a user would:
cargo doc --no-deps --open
cargo test --docLook at the rendered example first. Then run the doc tests to confirm the hidden lines still compile. A failing doc test prints the full source, hidden lines included, which helps when you cannot tell which line broke.
Common errors
- Writing
#use mycrate::add;without a space. Rustdoc does not treat it as hidden, and the line is a compile error. - Hiding a line and then forgetting it. When the API changes, the visible code and hidden code can drift apart. The doc test will catch compile errors, but not confusing explanations.
- Hiding too much. If most of an example is hidden, write a normal example instead.
Exercise
The parse_port example ends with the hidden line # Ok::<(), std::num::ParseIntError>(()). What does that line do, and why is it hidden?
Show answer
Doc tests are wrapped in a function that returns () unless the example ends with a Result. The final Ok::<(), ParseIntError>(()) expression gives the wrapper a Result return type, so ? in the visible code compiles. It is hidden because it is plumbing that does not teach the reader anything about parse_port.