Sub topic 1
Writing doc comments with /// and //!
Prerequisites
This chapter assumes you can write Rust functions, structs, and modules and mark items pub. It also assumes you can build a library crate with Cargo and that you know basic Markdown. Doc tests come in a later chapter, so this one only covers the comment syntax.
Two kinds of doc comment
Rust has two doc comment forms. Each one attaches to a different item.
| Syntax | Name | Documents |
|---|---|---|
| `///` | Outer doc comment | The item that follows it |
| `//!` | Inner doc comment | The item that contains it |
Both are Markdown text that rustdoc turns into HTML. Under the hood, each line is shorthand for a #[doc = "..."] attribute.
Outer comments with ///
Put /// directly above the item you want to describe. The first paragraph becomes the summary shown in item lists, so keep it to one short sentence.
/// A point in two-dimensional space.
///
/// Coordinates are measured in pixels from the top-left corner.
#[derive(Debug, Clone, Copy)]
pub struct Point {
/// Distance from the left edge.
pub x: i32,
/// Distance from the top edge.
pub y: i32,
}
/// Returns the point halfway between `a` and `b`.
pub fn midpoint(a: Point, b: Point) -> Point {
Point {
x: (a.x + b.x) / 2,
y: (a.y + b.y) / 2,
}
}You can document structs, enums, enum variants, fields, functions, traits, methods, constants, and type aliases. Attributes such as #[derive(Debug)] may sit between the comment and the item. A blank line between the comment and the item is also accepted.
Inner comments with //!
Use //! when the comment describes the thing it sits inside. The most common place is the top of src/lib.rs, where it documents the whole crate. The same syntax works at the top of a module file, or inside a mod name { ... } block.
//! # geometry
//!
//! Small helpers for working with points on a plane.
/// A point in two-dimensional space.
pub struct Point {
/// Distance from the left edge.
pub x: i32,
/// Distance from the top edge.
pub y: i32,
}Inner comments must come before any item in their scope. The crate-level text is the first thing a reader sees on the crate's front page.
Common errors
- Nothing to attach to. A
///at the end of a block, or before a closing brace, fails withexpected item after doc comment. - Inner comment too late. A
//!placed after an item fails because inner attributes must come first in their scope. - Doc comments on statements.
///above aletor an expression compiles but triggers theunused_doc_commentswarning. Use//for ordinary comments inside function bodies. - Four slashes.
////is a plain comment, not a doc comment. - Private items. Documentation on non-
pubitems is skipped by default.cargo doconly renders private items if you pass--document-private-items.
Writing the summary line
Treat the first line of every doc comment as a standalone sentence. It appears in module overviews and search results, away from the rest of your text. Start with a verb for functions ("Returns...", "Parses...") and a noun phrase for types ("A point in..."). Separate the summary from the details with a blank /// line.
Exercise
You want to describe an entire module that lives in src/shapes.rs. Which comment form do you use, and where do you put it?
Show answer
Use //! inner doc comments at the very top of src/shapes.rs, before any items. Inner comments document the enclosing item, which here is the module. Placing a /// comment above the mod shapes; declaration in the parent file also works.
Exercise
A function has the line /// Computes the total. placed just before the closing brace of its body, with no statement after it. What does the compiler report?
Show answer
It reports an error, expected item after doc comment, because an outer doc comment must be followed by an item to document.