Sub topic 2
Formatting documentation with Markdown
This chapter assumes you can write /// comments, build a library crate with Cargo, and read basic Markdown. Here you will see which Markdown rustdoc supports, how the first paragraph is used, and the mistakes that most often break rendered output.
How rustdoc reads Markdown
Rustdoc treats the text of every doc comment as CommonMark Markdown. It also supports a few extensions: tables, footnotes, strikethrough, and task lists. The leading /// and one following space are removed, then the remaining lines are parsed as Markdown.
/// Returns the larger of two values.
///
/// This function is **not** generic. It works on `i32` only.
///
/// - Returns `a` if `a >= b`.
/// - Returns `b` otherwise.
pub fn max_i32(a: i32, b: i32) -> i32 {
if a >= b { a } else { b }
}Backticks produce inline code, ** produces bold text, and - produces a bullet list. A blank /// line separates paragraphs.
The summary line
The first paragraph of a doc comment is the summary. Rustdoc shows it in module listings and search results, so keep it to one short sentence and end it with a period. Put details in later paragraphs.
/// Parses a decimal port number.
///
/// Leading and trailing whitespace is ignored. Values above 65535
/// are rejected.
pub fn parse_port(s: &str) -> Result<u16, std::num::ParseIntError> {
s.trim().parse()
}In the module index, readers see only "Parses a decimal port number." The rest appears on the item's own page.
Headings
Use # headings inside doc comments to create sections, such as # Examples. Rustdoc already uses <h1> for the item name, so start your own headings at # and use ## for subsections. The next chapter covers the conventional section names.
Tables and code blocks
Tables use the usual pipe syntax.
/// Converts a status code to a label.
///
/// | Code | Label |
/// |------|-------------|
/// | 200 | `"ok"` |
/// | 404 | `"missing"` |
/// | other| `"unknown"` |
pub fn label(code: u16) -> &'static str {
match code {
200 => "ok",
404 => "missing",
_ => "unknown",
}
}Fenced code blocks are special. A block with no language, or with rust, is treated as a Rust doc test. To show other content, name the language, for example text, toml, or sh. Rustdoc will not compile those blocks.
/// Reads settings from a file like this:
///
/// ```toml
/// port = 8080
/// ```
///
/// Example output:
///
/// ```text
/// listening on 0.0.0.0:8080
/// ```
pub fn load() {}Indented blocks (four spaces) are also treated as Rust code. Prefer fences, since they make the language explicit.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Doc test fails on non-Rust text | Fence has no language | Add `text`, `toml`, or `sh` |
| List renders as a paragraph | No blank `///` line before the list | Add an empty `///` line |
| Text is missing a leading space | Extra indentation turned it into a code block | Remove the extra indent |
| Heading looks too large | Used `#` for body text | Use `##` or bold text |
How the pieces fit together
The same code block feeds two outputs. It appears in the HTML, and cargo test --doc compiles and runs it. The next chapters build on this.
Exercise
A doc comment contains a fenced block with the log line listening on 0.0.0.0:8080 and no language after the opening fence. What happens when you run cargo test --doc, and how do you fix it?
Show answer
Rustdoc treats the unlabeled block as Rust code and tries to compile it, so the test fails with a syntax error. Add text after the opening fence, as in a fence opened with three backticks followed by text. Rustdoc then renders the block but does not compile it.
Exercise
Why should the first paragraph of a doc comment be a single short sentence?
Show answer
Rustdoc uses the first paragraph as the summary in module listings and search results. A short sentence keeps those lists readable, and the details go in later paragraphs on the item's page.