Sub topic 4
Documenting modules and crates
This chapter assumes you can write /// comments on functions and run cargo test --doc. You should also know how mod declarations and pub visibility work. Here you will document the containers that hold your items: the crate and its modules.
Outer and inner comments
A /// comment is an outer doc comment. It documents the item that comes after it. A //! comment is an inner doc comment. It documents the item that contains it. You can't write an outer comment for a crate, because nothing encloses the crate root. That is why //! exists.
| Comment | Documents | Where it goes |
|---|---|---|
| `///` | The next item | Immediately before the item |
| `//!` | The enclosing item | First lines inside a file or a `{ }` block |
Crate-level documentation
Put //! lines at the very top of src/lib.rs. rustdoc shows them as the front page of your documentation, and the first paragraph also becomes the crate's one-line summary in search results and listings.
The next example shows a complete lib.rs. Its fences inside the comments use ~~~ instead of backticks. CommonMark treats both as code fences, and rustdoc runs both as doc tests. Most real projects use backticks, but tildes keep this chapter's own code blocks intact.
//! # mycrate
//!
//! Small utilities for working with numbers.
//!
//! ~~~
//! let n = mycrate::add(1, 2);
//! assert_eq!(n, 3);
//! ~~~
pub mod stats {
//! Statistics helpers.
//!
//! ~~~
//! use mycrate::stats::mean;
//! assert_eq!(mean(&[1.0, 2.0, 3.0]), Some(2.0));
//! ~~~
/// Returns the arithmetic mean, or `None` for an empty slice.
pub fn mean(values: &[f64]) -> Option<f64> {
if values.is_empty() {
None
} else {
Some(values.iter().sum::<f64>() / values.len() as f64)
}
}
}
/// Adds two numbers.
pub fn add(a: i32, b: i32) -> i32 {
a + b
}Both //! blocks contain examples, so cargo test --doc runs them. The stats module has its inner comment as the first lines inside its braces.
Documenting a module in its own file
When a module lives in its own file, put the //! lines at the top of that file. Suppose src/lib.rs declares the module:
pub mod geometry;Then src/geometry.rs starts like this:
//! Shapes and measurements.
//!
//! Everything here works with `f64` lengths.
/// Area of a rectangle.
pub fn rect_area(w: f64, h: f64) -> f64 {
w * h
}You could instead write /// above pub mod geometry; in lib.rs. Both work, but pick one place per module. If you write both, rustdoc joins them, which is rarely what you want.
Where the text appears
Because the first paragraph is reused as a summary, write it as one short sentence that stands alone.
A useful structure
A good crate or module page has these parts, in order:
- A one-sentence summary.
- A short overview of what the crate or module is for and when to use it.
- A runnable example showing the most common use.
- Optional
#headings for deeper topics, such as feature flags or design notes.
Common errors
Inner comments must come before any item in their block. This file fails to compile:
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
//! Too late: this is an inner comment after an item.The compiler reports error E0753, "expected outer doc comment". Move the //! lines to the top, or change them to /// so they document a following item.
Another frequent mistake is writing crate-level examples with use crate::.... Each doc test is compiled as a separate program that depends on your crate, so use the crate's public name, mycrate::....
Exercise
You want a page-level description for a module stored in src/network.rs. Which comment style do you use, and where do you put it?
Show answer
Use //! inner comments on the first lines of src/network.rs, before any item. The first paragraph becomes the module's summary on the parent page. Putting /// above pub mod network; in lib.rs also works, but you shouldn't do both.