liveBook · Manning

Rustdoc

As explained in Idiomatic Rust

rustdoc is Rust's documentation generator that produces reference documentation from Rust source code and documentation comments.

rustdoc generates reference documentation from Rust source, turning public item names and documentation comments into browsable crate documentation. Good documentation helps library users understand and successfully use an API. section 6.11.2

Build documentation with Cargo, then improve it through focused Rust comments:

  • Run `cargo doc`; generated HTML appears under `target/doc`, with the crate index as its landing page. section 6.11.2
  • Use `//!` for crate-level documentation and `///` for the item immediately following the comment. section 6.11.2
  • Add high-level descriptions, usage examples, and concise explanations to public structs and methods. section 6.11.2
  • Use `cargo watch -x doc` to regenerate documentation automatically while editing. section 6.11.2

Documenting a crate

Place `//!` comments at the top of `src/lib.rs`, then show usage with a fenced Rust example: `use linkedlist::LinkedList; let mut animals = LinkedList::new(); animals.append("chicken");`. Document items with `///`, such as `/// Constructs a new, empty [`LinkedList<T>`].`. section 6.11.2

//! # linkedlist crate
//!
//! This crate provides a simple linked list implementation.
//!
//! The crate serves as a teaching example for the book [_Rust Advanced
//! Techniques_](https://www.manning.com/books/idiomatic-rust).
//!
//! ## Example usage
//!
//! ```rust
//! use linkedlist::LinkedList;
//!
//! let mut animals = LinkedList::new();
//! animals.append("chicken");
//! animals.append("ostrich");
//! animals.append("antelope");
//! animals.append("axolotl");
//! animals.append("okapi");
//! ```

Inspect a complete crate-level documentation listing with a description and example usage. section 6.11.2

How Idiomatic Rust covers it

How deep it goes
Explains HTML generation, documentation-comment scope, examples, and workflow automation, but does not descend into rustdoc internals.
Examples
Demonstrates a documented linked-list crate, generated HTML pages, usage snippets, and cargo test output containing doc tests.
What it assumes
Assumes familiarity with Rust crates, public items, structs, methods, and basic Cargo workflows.
What you can do after
Operate Cargo commands and documentation comments to generate and improve Rust library documentation.
How it connects
Connects API design with documentation comments, public items, crate documentation, and doc tests.

Related concepts

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