liveBook · Manning

Cargo doc

As explained in Learn Rust in a Month of Lunches

cargo doc is the Cargo command-line tool that generates documentation for Rust packages and their dependencies, with --open optionally opening the generated documentation.

cargo doc is Cargo’s command-line tool for generating browsable documentation for Rust packages and their dependencies. Running cargo doc --open builds the documentation and opens it in a browser, making unfamiliar Rust code easier to explore. section 13.1 section 18.4

Use cargo doc to:

  • Create documentation with a layout that is familiar across the standard library and external crates. section 18.4
  • Inspect structs, methods, automatically implemented traits, and blanket implementations. section 18.4
  • Display documentation comments written with /// above structs, functions, and methods. section 18.4
  • Use --no-deps when you want to document only your own code rather than its dependencies. section 18.4

Documenting a tiny crate

Add documentation comments to a small Rust API, then run cargo doc --open. For example, a DoesNothing struct and a PrintThing struct with a prints_something method appear in the generated pages. The documentation also reveals traits supplied automatically, while the /// comments explain the structs and method to readers. section 18.4

pub struct DoesNothing {}
pub struct PrintThing {}
 
impl PrintThing {
    pub fn prints_something() {
        println!("I am printing something");
    }
}

This minimal listing gives cargo doc a struct and a method to display in its generated documentation. section 18.4

How Learn Rust in a Month of Lunches covers it

How deep it goes
Explains generation, browser opening, API listings, comments, and dependency selection without covering documentation internals.
Examples
Uses DoesNothing, PrintThing, a printing method, automatic traits, blanket implementations, and /// comments.
What it assumes
Assumes Rust code and an installed Cargo environment, while teaching the documentation workflow from a simple project.
What you can do after
Apply it via Cargo to generate and inspect documentation for a Rust project or its dependencies.
How it connects
Connects Cargo with Rust documentation, structs, methods, traits, dependencies, and external crates.

Also explained in

Compare how 2 books explain cargo doc →

Related concepts

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