# 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! Let's create a new project with Cargo:

    $ cargo new docs
    $ cd docs

In `src/lib.rs`, you'll find that Cargo has generated some sample code. Delete it and replace it with this:

    # #![allow(unused_variables)]
    #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'll see a page with a search bar, and "Crate lib" at the top, with no contents. There's two problems with this: first, why does it think that our package 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's because our function `foo` is not public; `rustdoc` defaults to generating documentation for only public functions. If we change our code...

    # #![allow(unused_variables)]
    #fn main() {
    /// foo is a function
    pub fn foo() {}

    #}

... and then re-run `rustdoc`:

    $ rustdoc src/lib.rs --crate-name docs

We'll have some generated documentation. Open up `doc/docs/index.html` and check it out! It should show a link to the `foo` function's page, which is located at `doc/docs/fn.foo.html`. On that page, you'll see the "foo is a function" we put inside the documentation comment in our crate.

## Using rustdoc with Cargo

Cargo also has integration with `rustdoc` to make it easier to generate docs. Instead of the `rustdoc` command, we could have done this:

    $ cargo doc

Internally, this calls out to `rustdoc` like this:

    $ rustdoc --crate-name docs srclib.rs -o <path>\docs\target\doc -L
    dependency=<path>docs\target\debug\deps

You can see this with `cargo doc --verbose`.

It generates the correct `--crate-name` for us, as well as pointing to `src/lib.rs` But what about those other arguments? `-o` controls the output of our docs. Instead of a top-level `doc` directory, you'll notice that Cargo puts generated documentation under `target`. That's the idiomatic place for generated files in Cargo projects. Also, it passes `-L`, a flag that helps rustdoc find the dependencies your code relies on. If our project used dependencies, we'd get documentation for them as well!

## Using standalone Markdown files

`rustdoc` can also generate HTML from standalone Markdown files. Let's give it a try: create a `README.md` file with these contents:

        # Docs

        This is a project to test out `rustdoc`.

        [Here is a link!](https://www.rust-lang.org)

        ## Subheading

        ```rust
        fn foo() -> i32 {
            1 + 1
        }
        ```

And call `rustdoc` on it:

    $ rustdoc README.md

You'll find an HTML file in `docs/doc/README.html` generated from its Markdown contents.

Cargo currently does not understand standalone Markdown files, unfortunately.

## Summary

This covers the simplest use-cases of `rustdoc`. The rest of this book will explain all of the options that `rustdoc` has, and how to use them.

# Command-line arguments

Here's the list of arguments you can pass to `rustdoc`:

## `-h`/`--help`: help

Using this flag looks like this:

    $ rustdoc -h
    $ rustdoc --help

This will show `rustdoc`'s built-in help, which largely consists of a list of possible command-line flags.

Some of `rustdoc`'s flags are unstable; this page only shows stable options, `--help` will show them all.

## `-V`/`--version`: version information

Using this flag looks like this:

    $ rustdoc -V
    $ rustdoc --version

This will show `rustdoc`'s version, which will look something like this:

    rustdoc 1.17.0 (56124baa9 2017-04-24)

## `-v`/`--verbose`: more verbose output

Using this flag looks like this:

    $ rustdoc -v src/lib.rs
    $ rustdoc --verbose src/lib.rs

This enables "verbose mode", which means that more information will be written to standard out. What is written depends on the other flags you've passed in. For example, with `--version`:

    $ rustdoc --verbose --version
    rustdoc 1.17.0 (56124baa9 2017-04-24)
    binary: rustdoc
    commit-hash: hash
    commit-date: date
    host: host-triple
    release: 1.17.0
    LLVM version: 3.9

## `-r`/`--input-format`: input format

This flag is currently ignored; the idea is that `rustdoc` would support various input formats, and you could specify them via this flag.

Rustdoc only supports Rust source code and Markdown input formats. If the file ends in `.md` or `.markdown`, `rustdoc` treats it as a Markdown file. Otherwise, it assumes that the input file is Rust.

## `-w`/`--output-format`: output format

This flag is currently ignored; the idea is that `rustdoc` would support various output formats, and you could specify them via this flag.

Rustdoc only supports HTML output, and so this flag is redundant today.

## `-o`/`--output`: output path

Using this flag looks like this:

    $ rustdoc src/lib.rs -o target\\doc
    $ rustdoc src/lib.rs --output target\\doc

By default, `rustdoc`'s output appears in a directory named `doc` in the current working directory. With this flag, it will place all output into the directory you specify.

## `--crate-name`: controlling the name of the crate

Using this flag looks like this:

    $ rustdoc src/lib.rs --crate-name mycrate

By default, `rustodc` assumes that the name of your crate is the same name as the `.rs` file. `--crate-name` lets you override this assumption with whatever name you choose.

## `-L`/`--library-path`:

Using this flag looks like this:

    $ rustdoc src/lib.rs -L target/debug/deps
    $ rustdoc src/lib.rs --library-path target/debug/deps

If your crate has dependencies, `rustdoc` needs to know where to find them. Passing `--library-path` gives `rustdoc` a list of places to look for these dependencies.

This flag takes any number of directories as its argument, and will use all of them when searching.

## `--cfg`: passing configuration flags

Using this flag looks like this:

    $ rustdoc src/lib.rs --cfg feature="foo"

This flag accepts the same values as `rustc --cfg`, and uses it to configure compilation. The example above uses `feature`, but any of the `cfg` values are acceptable.

## `--extern`: specify a dependency's location

Using this flag looks like this:

    $ rustdoc src/lib.rs --extern lazy-static=/path/to/lazy-static

Similar to `--library-path`, `--extern` is about specifying the location of a dependency. `--library-path` provides directories to search in, `--extern` instead lets you specify exactly which dependency is located where.

## `--passes`: add more rustdoc passes

Using this flag looks like this:

    $ rustdoc --passes list
    $ rustdoc src/lib.rs --passes strip-priv-imports

An argument of "list" will print a list of possible "rustdoc passes", and other arguments will be the name of which passes to run in addition to the defaults.

For more details on passes, see the chapter on them.

See also `--no-defaults`.

## `--no-defaults`: don't run default passes

Using this flag looks like this:

    $ rustdoc src/lib.rs --no-defaults

By default, `rustdoc` will run several passes over your code. This removes those defaults, allowing you to use `--passes` to specify exactly which passes you want.

For more details on passes, see the chapter on them.

See also `--passes`.

## `--test`: run code examples as tests

Using this flag looks like this:

    $ rustdoc src/lib.rs --test

This flag will run your code examples as tests. For more, see the chapter on documentation tests.

See also `--test-args`.

## `--test-args`:

Using this flag looks like this:

    $ rustdoc src/lib.rs --test --test-args ignored

This flag will pass options to the test runner when running documentation tests. For more, see the chapter on documentation tests.

See also `--test`.

## `--target`:

Using this flag looks like this:

    $ rustdoc src/lib.rs --target x86_64-pc-windows-gnu

Similar to the `--target` flag for `rustc`, this generates documentation for a target triple that's different than your host triple.

All of the usual caveats of cross-compiling code apply.

## `--markdown-css`: include more CSS files when rendering markdown

Using this flag looks like this:

    $ rustdoc README.md --markdown-css foo.css

When rendering Markdown files, this will create a `<link>` element in the `<head>` section of the generated HTML. For example, with the invocation above,

    <link rel="stylesheet" type="text/css" href="foo.css">

will be added.

When rendering Rust files, this flag is ignored.

## `--html-in-header`: include more HTML in

Using this flag looks like this:

    $ rustdoc src/lib.rs --html-in-header header.html
    $ rustdoc README.md --html-in-header header.html

This flag takes a list of files, and inserts them into the `<head>` section of the rendered documentation.

## `--html-before-content`: include more HTML before the content

Using this flag looks like this:

    $ rustdoc src/lib.rs --html-before-content extra.html
    $ rustdoc README.md --html-before-content extra.html

This flag takes a list of files, and inserts them inside the `<body>` tag but before the other content `rustodc` would normally produce in the rendered documentation.

## `--html-after-content`: include more HTML after the content

Using this flag looks like this:

    $ rustdoc src/lib.rs --html-after-content extra.html
    $ rustdoc README.md --html-after-content extra.html

This flag takes a list of files, and inserts them before the `</body>` tag but after the other content `rustodc` would normally produce in the rendered documentation.

## `--markdown-playground-url`: control the location of the playground

Using this flag looks like this:

    $ rustdoc README.md --markdown-playground-url https://play.rust-lang.org/

When rendering a Markdown file, this flag gives the base URL of the Rust Playround, to use for generating `Run` buttons.

## `--markdown-no-toc`: don't generate a table of contents

Using this flag looks like this:

    $ rustdoc README.md --markdown-no-toc

When generating documentation from a Markdown file, by default, `rustdoc` will generate a table of contents. This flag supresses that, and no TOC will be generated.

## `-e`/`--extend-css`: extend rustdoc's CSS

Using this flag looks like this:

    $ rustdoc src/lib.rs -e extra.css
    $ rustdoc src/lib.rs --extend-css extra.css

With this flag, the contents of the files you pass are included at the bottom of Rustdoc's `theme.css` file.

While this flag is stable, the contents of `theme.css` are not, so be careful! Updates may break your theme extensions.

## `--sysroot`: override the system root

Using this flag looks like this:

    $ rustdoc src/lib.rs --sysroot /path/to/sysroot

Similar to `rustc --sysroot`, this lets you change the sysroot `rustdoc` uses when compiling your code.

# The `#[doc]` attribute

The `#[doc]` attribute lets you control various aspects of how `rustdoc` does its job.

The most basic function of `#[doc]` is to handle the actual documentation text. That is, `///` is syntax sugar for `#[doc]`. This means that these two are the same:

    /// This is a doc comment.
    #[doc = " This is a doc comment."]

(Note the leading space in the attribute version.)

In most cases, `///` is easier to use than `#[doc]`. One case where the latter is easier is when generating documentation in macros; the `collapse-docs` pass will combine multiple `#[doc]` attributes into a single doc comment, letting you generate code like this:

    #[doc = "This is"]
    #[doc = " a "]
    #[doc = "doc comment"]

Which can feel more flexible. Note that this would generate this:

    #[doc = "This is\n a \ndoc comment"]

but given that docs are rendered via Markdown, it will remove these newlines.

The `doc` attribute has more options though! These don't involve the text of the output, but instead, various aspects of the presentation of the output. We've split them into two kinds below: attributes that are useful at the crate level, and ones that are useful at the item level.

## At the crate level

These options control how the docs look at a macro level.

### `html_favicon_url`

This form of the `doc` attribute lets you control the favicon of your docs.

    #![doc(html_favicon_url = "https://example.com/favicon.ico")]

This will put `<link rel="shortcut icon" href="{}">` into your docs, where the string for the attribute goes into the `{}`.

If you don't use this attribute, there will be no favicon.

### `html_logo_url`

This form of the `doc` attribute lets you control the logo in the upper left hand side of the docs.

    #![doc(html_logo_url = "https://example.com/logo.jpg")]

This will put `<a href='index.html'><img src='{}' alt='logo' width='100'></a>` into your docs, where the string for the attribute goes into the `{}`.

If you don't use this attribute, there will be no logo.

### `html_playground_url`

This form of the `doc` attribute lets you control where the "run" buttons on your documentation examples make requests to.

    #![doc(html_playground_url = "https://playground.example.com/")]

Now, when you press "run", the button will make a request to this domain.

If you don't use this attribute, there will be no run buttons.

### `issue_tracker_base_url`

This form of the `doc` attribute is mostly only useful for the standard library; When a feature is unstable, an issue number for tracking the feature must be given. `rustdoc` uses this number, plus the base URL given here, to link to the tracking issue.

    #![doc(issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]

### `html_no_source`

By default, `rustdoc` will include the source code of your program, with links to it in the docs. But if you include this:

    #![doc(html_no_source)]

it will not.

## At the item level

These forms of the `#[doc]` attribute are used on individual items, to control how they are documented.

## `#[doc(no_inline)]`/`#[doc(inline)]`

These attributes are used on `use` statements, and control where the documentation shows up. For example, consider this Rust code:

    pub use bar::Bar;

    /// bar docs
    pub mod bar {
        /// the docs for Bar
        pub struct Bar;
    }

The documentation will generate a "Reexports" section, and say `pub use bar::Bar;`, where `Bar` is a link to its page.

If we change the `use` line like this:

    #[doc(inline)]
    pub use bar::Bar;

Instead, `Bar` will appear in a `Structs` section, just like `Bar` was defined at the top level, rather than `pub use`'d.

Let's change our original example, by making `bar` private:

    pub use bar::Bar;

    /// bar docs
    mod bar {
        /// the docs for Bar
        pub struct Bar;
    }

Here, because `bar` is not public, `Bar` wouldn't have its own page, so there's nowhere to link to. `rustdoc` will inline these definitions, and so we end up in the same case as the `#[doc(inline)]` above; `Bar` is in a `Structs` section, as if it were defined at the top level. If we add the `no_inline` form of the attribute:

    #[doc(no_inline)]
    pub use bar::Bar;

    /// bar docs
    mod bar {
        /// the docs for Bar
        pub struct Bar;
    }

Now we'll have a `Reexports` line, and `Bar` will not link to anywhere.

## `#[doc(hidden)]`

Any item annotated with `#[doc(hidden)]` will not appear in the documentation, unless the `strip-hidden` pass is removed.

## `#[doc(primitive)]`

Since primitive types are defined in the compiler, there's no place to attach documentation attributes. This attribute is used by the standard library to provide a way to generate documentation for primitive types.

# Documentation tests

`rustdoc` supports executing your documentation examples as tests. This makes sure that your tests are up to date and working.

The basic idea is this:

    /// # Examples
    ///
    /// ```
    /// let x = 5;
    /// ```

The triple backticks start and end code blocks. If this were in a file named `foo.rs`, running `rustdoc --test foo.rs` will extract this example, and then run it as a test.

There's some subtlety though! Read on for more details.

## Pre-processing examples

In the example above, you'll note something strange: there's no `main` function! Forcing you to write `main` for every example, no matter how small, adds friction. So `rustdoc` processes your examples slightly before running them. Here's the full algorithm rustdoc uses to preprocess examples:

1. Any leading `#![foo]` attributes are left intact as crate attributes.
2. Some common `allow` attributes are inserted, including `unused_variables`, `unused_assignments`, `unused_mut`, `unused_attributes`, and `dead_code`. Small examples often trigger these lints.
3. If the example does not contain `extern crate`, then `extern crate <mycrate>;` is inserted (note the lack of `#[macro_use]`).
4. Finally, if the example does not contain `fn main`, the remainder of the text is wrapped in `fn main() { your_code }`.

For more about that caveat in rule 3, see "Documeting Macros" below.

## Hiding portions of the example

Sometimes, you need some setup code, or other things that would distract from your example, but are important to make the tests work. Consider an example block that looks like this:

    /// Some documentation.
    # fn foo() {}

It will render like this:

    # #![allow(unused_variables)]
    #fn main() {
    /// Some documentation.
    # fn foo() {}

    #}

Yes, that's right: you can add lines that start with `#`, and they will be hidden from the output, but will be used when compiling your code. You can use this to your advantage. In this case, documentation comments need to apply to some kind of function, so if I want to show you just a documentation comment, I need to add a little function definition below it. At the same time, it's only there to satisfy the compiler, so hiding it makes the example more clear. You can use this technique to explain longer examples in detail, while still preserving the testability of your documentation.

For example, imagine that we wanted to document this code:

    # #![allow(unused_variables)]
    #fn main() {
    let x = 5;
    let y = 6;
    println!("{}", x + y);

    #}

We might want the documentation to end up looking like this:

> First, we set `x` to five:

>     # #![allow(unused_variables)]
>     #fn main() {
>     let x = 5;
>     # let y = 6;
>     # println!("{}", x + y);

>     #}

> Next, we set `y` to six:

>     # #![allow(unused_variables)]
>     #fn main() {
>     # let x = 5;
>     let y = 6;
>     # println!("{}", x + y);

>     #}

> Finally, we print the sum of `x` and `y`:

>     # #![allow(unused_variables)]
>     #fn main() {
>     # let x = 5;
>     # let y = 6;
>     println!("{}", x + y);

>     #}

To keep each code block testable, we want the whole program in each block, but we don't want the reader to see every line every time. Here's what we put in our source code:

        First, we set `x` to five:

        ```rust
        let x = 5;
        # let y = 6;
        # println!("{}", x + y);
        ```

        Next, we set `y` to six:

        ```rust
        # let x = 5;
        let y = 6;
        # println!("{}", x + y);
        ```

        Finally, we print the sum of `x` and `y`:

        ```rust
        # let x = 5;
        # let y = 6;
        println!("{}", x + y);
        ```

By repeating all parts of the example, you can ensure that your example still compiles, while only showing the parts that are relevant to that part of your explanation.

Another case where the use of `#` is handy is when you want to ignore error handling. Lets say you want the following,

    /// use std::io;
    /// let mut input = String::new();
    /// io::stdin().read_line(&mut input)?;

The problem is that `?` returns a `Result<T, E>` and test functions don't return anything so this will give a mismatched types error.

    /// A doc test using ?
    ///
    /// ```
    /// use std::io;
    /// # fn foo() -> io::Result<()> {
    /// let mut input = String::new();
    /// io::stdin().read_line(&mut input)?;
    /// # Ok(())
    /// # }
    /// ```
    # fn foo() {}

You can get around this by wrapping the code in a function. This catches and swallows the `Result<T, E>` when running tests on the docs. This pattern appears regularly in the standard library.

### Documenting macros

Here’s an example of documenting a macro:

    /// Panic with a given message unless an expression evaluates to true.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[macro_use] extern crate foo;
    /// # fn main() {
    /// panic_unless!(1 + 1 == 2, “Math is broken.”);
    /// # }
    /// ```
    ///
    /// ```rust,should_panic
    /// # #[macro_use] extern crate foo;
    /// # fn main() {
    /// panic_unless!(true == false, “I’m broken.”);
    /// # }
    /// ```
    #[macro_export]
    macro_rules! panic_unless {
        ($condition:expr, $($rest:expr),+) => ({ if ! $condition { panic!($($rest),+); } });
    }
    # fn main() {}

You’ll note three things: we need to add our own `extern crate` line, so that we can add the `#[macro_use]` attribute. Second, we’ll need to add our own `main()` as well (for reasons discussed above). Finally, a judicious use of `#` to comment out those two things, so they don’t show up in the output.

## Attributes

There are a few annotations that are useful to help `rustdoc` do the right thing when testing your code:

    # #![allow(unused_variables)]
    #fn main() {
    /// ```ignore
    /// fn foo() {
    /// ```
    # fn foo() {}

    #}

The `ignore` directive tells Rust to ignore your code. This is almost never what you want, as it's the most generic. Instead, consider annotating it with `text` if it's not code, or using `#`s to get a working example that only shows the part you care about.

    # #![allow(unused_variables)]
    #fn main() {
    /// ```should_panic
    /// assert!(false);
    /// ```
    # fn foo() {}

    #}

`should_panic` tells `rustdoc` that the code should compile correctly, but not actually pass as a test.

    # #![allow(unused_variables)]
    #fn main() {
    /// ```no_run
    /// loop {
    ///     println!("Hello, world");
    /// }
    /// ```
    # fn foo() {}

    #}

The `no_run` attribute will compile your code, but not run it. This is important for examples such as "Here's how to retrieve a web page," which you would want to ensure compiles, but might be run in a test environment that has no network access.

# Passes

Rustdoc has a concept called "passes". These are transformations that `rustdoc` runs on your documentation before producing its final output.

In addition to the passes below, check out the docs for these flags:

- `--passes`
- `--no-defaults`

## Default passes

By default, rustdoc will run some passes, namely:

- `strip-hidden`
- `strip-private`
- `collapse-docs`
- `unindent-comments`

However, `strip-private` implies `strip-private-imports`, and so effectively, all passes are run by default.

## `strip-hidden`

This pass implements the `#[doc(hidden)]` attribute. When this pass runs, it checks each item, and if it is annotated with this attribute, it removes it from `rustdoc`'s output.

Without this pass, these items will remain in the output.

## `unindent-comments`

When you write a doc comment like this:

    /// This is a documentation comment.

There's a space between the `///` and that `T`. That spacing isn't intended to be a part of the output; it's there for humans, to help separate the doc comment syntax from the text of the comment. This pass is what removes that space.

The exact rules are left under-specified so that we can fix issues that we find.

Without this pass, the exact number of spaces is preserved.

## `collapse-docs`

With this pass, multiple `#[doc]` attributes are converted into one single documentation string.

For example:

    #[doc = "This is the first line."]
    #[doc = "This is the second line."]

Gets collapsed into a single doc string of

    This is the first line.
    This is the second line.

## `strip-private`

This removes documentation for any non-public items, so for example:

    /// These are private docs.
    struct Private;

    /// These are public docs.
    pub struct Public;

This pass removes the docs for `Private`, since they're not public.

This pass implies `strip-priv-imports`.

## `strip-priv-imports`

This is the same as `strip-private`, but for `extern crate` and `use` statements instead of items.