Sub topic 6
Building and viewing docs with cargo doc
This chapter assumes you can build a library crate with Cargo and that you have written /// comments on public items. You will build the HTML documentation for your crate, open it in a browser, and learn which flags change what gets built.
What cargo doc does
cargo doc runs rustdoc on every crate in your dependency graph. Rustdoc reads the source, collects the doc comments, and writes static HTML into target/doc. Cargo passes rustdoc the right --extern flags and edition, so you rarely call rustdoc directly.
The most useful form for daily work is:
cargo doc --no-deps --open--no-depsdocuments only your own crate. Without it, Cargo also builds docs for every dependency, which is slow and mostly unnecessary.--openopens the result in your default browser after the build finishes.
The entry page is target/doc/<crate_name>/index.html. Hyphens in the package name become underscores in the directory name, so a package named my-crate appears under my_crate.
The build flow
What appears in the output
Rustdoc documents only public items by default. A private function with a perfect doc comment will not show up. To see private items while working on internals, add a flag:
cargo doc --no-deps --document-private-itemsThe generated page has three parts worth knowing:
- A sidebar listing modules, structs, enums, traits, and functions.
- A search box, opened by pressing
S, that searches names and signatures. - A
[src]link on each item that shows the source code.
Rustdoc also adds trait implementations to each type's page, including implementations that come from derives and blanket impls. You do not write these sections yourself.
Useful flags
| Flag | Effect |
|---|---|
| `--no-deps` | Document only workspace crates |
| `--open` | Open the docs in a browser when the build finishes |
| `--document-private-items` | Include private items in the output |
| `--all-features` | Build with every Cargo feature enabled |
| `--workspace` | Document every member of a workspace |
| `-p <name>` | Document a single package |
In a workspace, plain cargo doc documents the package in the current directory. Use --workspace when you want all members linked together.
Docs are not tests
cargo doc does not run your examples. It only renders them. A broken example compiles into perfectly good-looking HTML. Run the tests separately:
cargo test --docTreat the two commands as a pair. One checks how the documentation reads, the other checks that its code works.
Common problems
- Warnings about unresolved links. Rustdoc reports a broken intra-doc link as a warning, and the build still succeeds. Read the warnings, because they point at links that will be dead in the output.
- Stale output. Rustdoc rebuilds based on file changes, but if a page looks outdated, run
cargo clean --docto removetarget/docand rebuild. - Missing item. If an item is absent, check its visibility first. Then check for a
#[doc(hidden)]attribute, which removes it from the output on purpose. - Crate name confusion. The page you want is under the library crate name, not the package name, if they differ.
Exercises
Exercise
You run cargo doc in a project with 40 dependencies and the build takes a long time. Which flag limits the build to your own crate, and why does it help?
Show answer
--no-deps. Without it, Cargo runs rustdoc on every dependency as well. With it, rustdoc processes only your crate, so the build is much shorter.
Exercise
A private helper function has a detailed /// comment, but it does not appear in the generated docs. How do you make it appear, and does it change how cargo test --doc behaves?
Show answer
Run cargo doc --document-private-items. This only changes the rendered HTML. Doc tests are collected separately by cargo test --doc, so the flag has no effect on them.