What is rustdoc?
The standard Rust distribution ships with a tool called rustdoc. Its job is
to generate documentation for Rust projects. On a fundamental level, Rustdoc
takes as an argument either a crate root or a Markdown file, and produces HTML,
CSS, and JavaScript.
Basic usage
Let’s give it a try! Create a new project with Cargo:
$ cargo new docs --lib
$ cd docs
In src/lib.rs, Cargo has generated some sample code. Delete
it and replace it with this:
#![allow(unused)]
fn main() {
/// foo is a function
fn foo() {}
}
Let’s run rustdoc on our code. To do so, we can call it with the path to
our crate root like this:
$ rustdoc src/lib.rs
This will create a new directory, doc, with a website inside! In our case,
the main page is located in doc/lib/index.html. If you open that up in
a web browser, you will see a page with a search bar, and “Crate lib” at the
top, with no contents.
You can also use cargo doc to generate documentation for the whole project.
See Using rustdoc with Cargo.
Configuring rustdoc
There are two problems with this: first, why does it think that our crate is named “lib”? Second, why does it not have any contents?
The first problem is due to rustdoc trying to be helpful; like rustc,
it assumes that our crate’s name is the name of the file for the crate
root. To fix this, we can pass in a command-line flag:
$ rustdoc src/lib.rs --crate-name docs
Now, doc/docs/index.html will be generated, and the page says “Crate docs.”
For the second issue, it is because our function foo is not publ