liveBook · Manning

Doc comment

As explained in Refactoring to Rust

A doc comment is a Rust comment written with three slashes that is attached to an item and processed as API documentation.

A doc comment is a Rust comment beginning with three slashes (`///`). Attached to an item, it becomes part of the crate’s generated API documentation instead of remaining an ordinary source comment. section 7.1.1

Doc comments support several practical documentation and testing features:

  • Describe public items and render their type signatures and explanations in generated documentation. section 7.1.1
  • Use Markdown for lists, links, emphasis, and other formatting. section 7.1.1
  • Include fenced code examples that compile as separate crates and access only the crate’s public API. section 7.1.1
  • Run documentation examples with `cargo test`, alongside ordinary tests. section 7.1.1 section 7.3

Documenting and testing `add`

/// Add together two i32 numbers and return the result of that addition /// ``` /// assert_eq!(testing::add(2, 2), 4); /// ``` pub fn add(x: i32, y: i32) -> i32 { x + y } A passing example validates the documented public API. Incorrect assertions fail, while invalid argument types fail to compile.

How Refactoring to Rust covers it

How deep it goes
Explains syntax, generated output, Markdown, public-API isolation, and passing, failing, and non-compiling documentation tests.
Examples
Uses an `add` function, generated documentation, Markdown code blocks, and assertion and type-error documentation tests.
What it assumes
Assumes basic Rust functions and tests while teaching documentation comments and documentation tests incrementally from ordinary comments.
What you can do after
Apply doc comments, generate crate documentation, and validate public-API examples through `cargo test`.
How it connects
Connects doc comments with Markdown, public APIs, documentation tests, `cargo doc`, and `cargo test`.

Also explained in

Compare how 2 books explain doc comment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.