The rustdoc book

# 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
    $ 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.

## Configuring rustdoc

There are 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 is because our function `foo` is not public; `rustdoc` defaults to generating documentation for only public functions. If we change our code...

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

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

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

We now 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 src/lib.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, notice that Cargo puts generated documentation under `target`. That is the idiomatic place for generated files in Cargo projects.
- `-L` flag helps rustdoc find the dependencies your code relies on. If our project used dependencies, we would get documentation for them as well!

## Outer and inner documentation

The `///` syntax is used to document the item present after it. That's why it is called an outer documentation. There is another syntax: `//!`, which is used to document the item it is present inside. It is called an inner documentation. It is often used when documenting the entire crate, because nothing comes before it: it is the root of the crate. So in order to document an entire crate, you need to use `//!` syntax. For example:

    #![allow(unused)]
    fn main() {
    //! This is my first rust crate
    }

When used in the crate root, it documents the item it is inside, which is the crate itself.

For more information about the `//!` syntax, see the Book.

## 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)

    ## Example

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

And call `rustdoc` on it:

    $ rustdoc README.md

You will 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.

# How to write documentation

Good documentation is not natural. There are opposing goals that make writing good documentation difficult. It requires expertise in the subject but also writing to a novice perspective. Documentation therefore often glazes over implementation detail, or leaves readers with unanswered questions.

There are a few tenets to Rust documentation that can help guide anyone through the process of documenting libraries so that everyone has an ample opportunity to use the code.

This chapter covers not only how to write documentation but specifically how to write good documentation. It is important to be as clear as you can, and as complete as possible. As a rule of thumb: the more documentation you write for your crate the better. If an item is public then it should be documented.

## Getting Started

Documenting a crate should begin with front-page documentation. As an example, the `hashbrown` crate level documentation summarizes the role of the crate, provides links to explain technical details, and explains why you would want to use the crate.

After introducing the crate, it is important that the front-page gives an example of how to use the crate in a real world setting. Stick to the library's role in the example, but do so without shortcuts to benefit users who may copy and paste the example to get started.

`futures` uses inline comments to explain line by line the complexities of using a `Future`, because a person's first exposure to rust's `Future` may be this example.

The `backtrace` documentation walks through the whole process, explaining changes made to the `Cargo.toml` file, passing command line arguments to the compiler, and shows a quick example of backtrace in the wild.

Finally, the front-page can eventually become a comprehensive reference how to use a crate, like `regex`. In this front page, all requirements are outlined, the edge cases shown, and practical examples provided. The front page goes on to show how to use regular expressions then concludes with crate features.

Don't worry about comparing your crate, which is just beginning, to other more developed crates. To get the documentation to something more polished, start incrementally and put in an introduction, example, and features. Rome was not built in a day!

The first lines within the `lib.rs` will compose the front-page, and they use a different convention than the rest of the rustdocs. Lines should start with `//!` which indicate module-level or crate-level documentation. Here's a quick example of the difference:

    //! Fast and easy queue abstraction.
    //!
    //! Provides an abstraction over a queue.  When the abstraction is used
    //! there are these advantages:
    //! - Fast
    //! - [`Easy`]
    //!
    //! [`Easy`]: http://thatwaseasy.example.com

    /// This module makes it easy.
    pub mod easy {

        /// Use the abstract function to do this specific thing.
        pub fn abstract() {}

    }

Ideally, this first line of documentation is a sentence without highly technical details, but with a good description of where this crate fits within the rust ecosystem. Users should know whether this crate meets their use case after reading this line.

## Documenting components

Whether it is modules, structs, functions, or macros: the public API of all code should have documentation. Rarely does anyone complain about too much documentation!

It is recommended that each item's documentation follows this basic structure:

    [short sentence explaining what it is]

    [more detailed explanation]

    [at least one code example that users can copy/paste to try it]

    [even more advanced explanations if necessary]

This basic structure should be straightforward to follow when writing your documentation; while you might think that a code example is trivial, the examples are really important because they can help users understand what an item is, how it is used, and for what purpose it exists.

Let's see an example coming from the standard library by taking a look at the `std::env::args()` function:

    Returns the arguments which this program was started with (normally passed
    via the command line).

    The first element is traditionally the path of the executable, but it can be
    set to arbitrary text, and may not even exist. This means this property should
    not be relied upon for security purposes.

    On Unix systems shell usually expands unquoted arguments with glob patterns
    (such as `*` and `?`). On Windows this is not done, and such arguments are
    passed as-is.

    # Panics

    The returned iterator will panic during iteration if any argument to the
    process is not valid unicode. If this is not desired,
    use the [`args_os`] function instead.

    # Examples

    ```
    use std::env;

    // Prints each argument on a separate line
    for argument in env::args() {
        println!("{}", argument);
    }
    ```

    [`args_os`]: ./fn.args_os.html

Everything before the first empty line will be reused to describe the component in searches and module overviews. For example, the function `std::env::args()` above will be shown on the `std::env` module documentation. It is good practice to keep the summary to one line: concise writing is a goal of good documentation.

Because the type system does a good job of defining what types a function passes and returns, there is no benefit of explicitly writing it into the documentation, especially since `rustdoc` adds hyper links to all types in the function signature.

In the example above, a 'Panics' section explains when the code might abruptly exit, which can help the reader prevent reaching a panic. A panic section is recommended every time edge cases in your code can be reached if known.

As you can see, it follows the structure detailed above: it starts with a short sentence explaining what the functions does, then it provides more information and finally provides a code example.

## Markdown

`rustdoc` uses the commonmark markdown specification. You might be interested into taking a look at their website to see what's possible to do.

- commonmark quick reference
- current spec

# What to include (and exclude)

It is easy to say everything must be documented in a project and often times that is correct, but how can we get there, and are there things that don't belong?

At the top of the `src/lib.rs` or `main.rs` file in your binary project, include the following attribute:

    #![allow(unused)]
    #![warn(missing_docs)]
    fn main() {
    }

Now run `cargo doc` and examine the output. Here's a sample:

     Documenting docdemo v0.1.0 (/Users/username/docdemo)
    warning: missing documentation for the crate
     --> src/main.rs:1:1
      |
    1 | / #![warn(missing_docs)]
    2 | |
    3 | | fn main() {
    4 | |     println!("Hello, world!");
    5 | | }
      | |_^
      |
    note: the lint level is defined here
     --> src/main.rs:1:9
      |
    1 | #![warn(missing_docs)]
      |         ^^^^^^^^^^^^

    warning: 1 warning emitted

        Finished dev [unoptimized + debuginfo] target(s) in 2.96s

As a library author, adding the lint `#![deny(missing_docs)]` is a great way to ensure the project does not drift away from being documented well, and `#![warn(missing_docs)]` is a good way to move towards comprehensive documentation. In addition to docs, `#![deny(missing_doc_code_examples)]` ensures each function contains a usage example. In our example above, the warning is resolved by adding crate level documentation.

There are more lints in the upcoming chapter Lints.

## Examples

Of course this is contrived to be simple, but part of the power of documentation is showing code that is easy to follow, rather than being realistic. Docs often take shortcuts with error handling because examples can become complicated to follow with all the necessary set up required for a simple example.

`Async` is a good example of this. In order to execute an `async` example, an executor needs to be available. Examples will often shortcut this, and leave users to figure out how to put the `async` code into their own runtime.

It is preferred that `unwrap()` not be used inside an example, and some of the error handling components be hidden if they make the example too difficult to follow.

    /// Example
    /// ```rust
    /// let fourtytwo = "42".parse::<u32>()?;
    /// println!("{} + 10 = {}", fourtytwo, fourtytwo+10);
    /// ```

When rustdoc wraps that in a main function, it will fail to compile because the `ParseIntError` trait is not implemented. In order to help both your audience and your test suite, this example needs some additional code:

    /// Example
    /// ```rust
    /// # main() -> Result<(), std::num::ParseIntError> {
    /// let fortytwo = "42".parse::<u32>()?;
    /// println!("{} + 10 = {}", fortytwo, fortytwo+10);
    /// #     Ok(())
    /// # }
    /// ```

The example is the same on the doc page, but has that extra information available to anyone trying to use your crate. More about tests in the upcoming Documentation tests chapter.

## What to Exclude

Certain parts of your public interface may be included by default in the output of rustdoc. The attribute `#[doc(hidden)]` can hide implementation details to encourage idiomatic use of the crate.

For example, an internal `macro!` that makes the crate easier to implement can become a footgun for users when it appears in the public documentation. An internal `Error` type may exist, and `impl` details should be hidden, as detailed in the API Guidelines.

## Customizing the output

It is possible to pass a custom css file to `rustdoc` and style the documentation.

    rustdoc --extend-css custom.css src/lib.rs

A good example of using this feature to create a dark theme is documented on this blog. Just remember, dark theme is already included in the rustdoc output by clicking on the paintbrush. Adding additional options to the themes are as easy as creating a custom theme `.css` file and using the following syntax:

    rustdoc --theme awesome.css src/lib.rs

Here is an example of a new theme, Ayu.

# 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, `rustdoc` 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`: where to look for dependencies

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.

## `-C`/`--codegen`: pass codegen options to rustc

Using this flag looks like this:

    $ rustdoc src/lib.rs -C target_feature=+avx
    $ rustdoc src/lib.rs --codegen target_feature=+avx

    $ rustdoc --test src/lib.rs -C target_feature=+avx
    $ rustdoc --test src/lib.rs --codegen target_feature=+avx

    $ rustdoc --test README.md -C target_feature=+avx
    $ rustdoc --test README.md --codegen target_feature=+avx

When rustdoc generates documentation, looks for documentation tests, or executes documentation tests, it needs to compile some rust code, at least part-way. This flag allows you to tell rustdoc to provide some extra codegen options to rustc when it runs these compilations. Most of the time, these options won't affect a regular documentation run, but if something depends on target features to be enabled, or documentation tests need to use some additional options, this flag allows you to affect that.

The arguments to this flag are the same as those for the `-C` flag on rustc. Run `rustc -C help` to get the full list.

## `--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`: pass options to test runner

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`: generate documentation for the specified target triple

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 `rustdoc` 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 `rustdoc` 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 Playground, 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 suppresses 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.

### `--edition`: control the edition of docs and doctests

Using this flag looks like this:

    $ rustdoc src/lib.rs --edition 2018
    $ rustdoc --test src/lib.rs --edition 2018

This flag allows `rustdoc` to treat your rust code as the given edition. It will compile doctests with the given edition as well. As with `rustc`, the default edition that `rustdoc` will use is `2015` (the first edition).

## `--theme`: add a theme to the documentation output

Using this flag looks like this:

    $ rustdoc src/lib.rs --theme /path/to/your/custom-theme.css

`rustdoc`'s default output includes two themes: `light` (the default) and `dark`. This flag allows you to add custom themes to the output. Giving a CSS file to this flag adds it to your documentation as an additional theme choice. The theme's name is determined by its filename; a theme file named `custom-theme.css` will add a theme named `custom-theme` to the documentation.

## `--check-theme`: verify custom themes against the default theme

Using this flag looks like this:

    $ rustdoc --check-theme /path/to/your/custom-theme.css

While `rustdoc`'s HTML output is more-or-less consistent between versions, there is no guarantee that a theme file will have the same effect. The `--theme` flag will still allow you to add the theme to your documentation, but to ensure that your theme works as expected, you can use this flag to verify that it implements the same CSS rules as the official `light` theme.

`--check-theme` is a separate mode in `rustdoc`. When `rustdoc` sees the `--check-theme` flag, it discards all other flags and only performs the CSS rule comparison operation.

### `--crate-version`: control the crate version

Using this flag looks like this:

    $ rustdoc src/lib.rs --crate-version 1.3.37

When `rustdoc` receives this flag, it will print an extra "Version (version)" into the sidebar of the crate root's docs. You can use this flag to differentiate between different versions of your library's documentation.

# 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 crate 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_root_url`

The `#[doc(html_root_url = "…")]` attribute value indicates the URL for generating links to external crates. When rustdoc needs to generate a link to an item in an external crate, it will first check if the extern crate has been documented locally on-disk, and if so link directly to it. Failing that, it will use the URL given by the `--extern-html-root-url` command-line flag if available. If that is not available, then it will use the `html_root_url` value in the extern crate if it is available. If that is not available, then the extern items will not be linked.

    #![doc(html_root_url = "https://docs.rs/serde/1.0")]

### `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.

### `test(no_crate_inject)`

By default, `rustdoc` will automatically add a line with `extern crate my_crate;` into each doctest. But if you include this:

    #![doc(test(no_crate_inject))]

it will not.

### `test(attr(...))`

This form of the `doc` attribute allows you to add arbitrary attributes to all your doctests. For example, if you want your doctests to fail if they produce any warnings, you could add this:

    #![doc(test(attr(deny(warnings))))]

## 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 "Re-exports" 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 `Re-exports` line, and `Bar` will not link to anywhere.

One special case: In Rust 2018 and later, if you `pub use` one of your dependencies, `rustdoc` will not eagerly inline it as a module unless you add `#[doc(inline)]`.

## `#[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 examples within your documentation 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.

Please note that by default, if no language is set for the block code, rustdoc assumes it is Rust code. So the following:

    ```rust
    let x = 5;
    ```

is strictly equivalent to:

    ```
    let x = 5;
    ```

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

## Passing or failing a doctest

Like regular unit tests, regular doctests are considered to "pass" if they compile and run without panicking. So if you want to demonstrate that some computation gives a certain result, the `assert!` family of macros works the same as other Rust code:

    #![allow(unused)]
    fn main() {
    let foo = "foo";
    assert_eq!(foo, "foo");
    }

This way, if the computation ever returns something different, the code panics and the doctest fails.

## 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 and clutters the output. So `rustdoc` processes your examples slightly before running them. Here's the full algorithm `rustdoc` uses to preprocess examples:

1. Some common `allow` attributes are inserted, including `unused_variables`, `unused_assignments`, `unused_mut`, `unused_attributes`, and `dead_code`. Small examples often trigger these lints.
2. Any attributes specified with `#![doc(test(attr(...)))]` are added.
3. Any leading `#![foo]` attributes are left intact as crate attributes.
4. If the example does not contain `extern crate`, and `#![doc(test(no_crate_inject))]` was not specified, then `extern crate <mycrate>;` is inserted (note the lack of `#[macro_use]`).
5. 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 4, see "Documenting 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() {} // this function will be hidden
    /// println!("Hello, World!");
    /// ```

It will render like this:

    #![allow(unused)]
    fn main() {
    /// Some documentation.
    fn foo() {}
    println!("Hello, World!");
    }

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)]
    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)]
>     fn main() {
>     let x = 5;
>     let y = 6;
>     println!("{}", x + y);
>     }

> Next, we set `y` to six:

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

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

>     #![allow(unused)]
>     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:

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

    Next, we set `y` to six:

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

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

    ```
    # 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.

The `#`-hiding of lines can be prevented by using two consecutive hashes `##`. This only needs to be done with the first `#` which would've otherwise caused hiding. If we have a string literal like the following, which has a line that starts with a `#`:

    #![allow(unused)]
    fn main() {
    let s = "foo
    # bar # baz";
    }

We can document it by escaping the initial `#`:

    /// let s = "foo
    /// ## bar # baz";

## Using `?` in doc tests

When writing an example, it is rarely useful to include a complete error handling, as it would add significant amounts of boilerplate code. Instead, you may 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.

You can get around this limitation by manually adding a `main` that returns `Result<T, E>`, because `Result<T, E>` implements the `Termination` trait:

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

Together with the `#` from the section above, you arrive at a solution that appears to the reader as the initial idea but works with doc tests:

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

As of version 1.34.0, one can also omit the `fn main()`, but you will have to disambiguate the error type:

    /// ```
    /// use std::io;
    /// let mut input = String::new();
    /// io::stdin().read_line(&mut input)?;
    /// # Ok::<(), io::Error>(())
    /// ```

This is an unfortunate consequence of the `?` operator adding an implicit conversion, so type inference fails because the type is not unique. Please note that you must write the `(())` in one sequence without intermediate whitespace so that `rustdoc` understands you want an implicit `Result`-returning function.

## 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.”);
    /// # }
    /// ```
    ///
    /// ```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)]
    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)]
    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)]
    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.

    /// ```compile_fail
    /// let x = 5;
    /// x += 2; // shouldn't compile!
    /// ```

`compile_fail` tells `rustdoc` that the compilation should fail. If it compiles, then the test will fail. However please note that code failing with the current Rust release may work in a future release, as new features are added.

    /// Only runs on the 2018 edition.
    ///
    /// ```edition2018
    /// let result: Result<i32, ParseIntError> = try {
    ///     "1".parse::<i32>()?
    ///         + "2".parse::<i32>()?
    ///         + "3".parse::<i32>()?
    /// };
    /// ```

`edition2018` tells `rustdoc` that the code sample should be compiled using the 2018 edition of Rust. Similarly, you can specify `edition2015` to compile the code with the 2015 edition.

## Syntax reference

The exact syntax for code blocks, including the edge cases, can be found in the Fenced Code Blocks section of the CommonMark specification.

Rustdoc also accepts indented code blocks as an alternative to fenced code blocks: instead of surrounding your code with three backticks, you can indent each line by four or more spaces.

        let foo = "foo";
        assert_eq!(foo, "foo");

These, too, are documented in the CommonMark specification, in the Indented Code Blocks section.

However, it's preferable to use fenced code blocks over indented code blocks. Not only are fenced code blocks considered more idiomatic for Rust code, but there is no way to use directives such as `ignore` or `should_panic` with indented code blocks.

### Include items only when collecting doctests

Rustdoc's documentation tests can do some things that regular unit tests can't, so it can sometimes be useful to extend your doctests with samples that wouldn't otherwise need to be in documentation. To this end, Rustdoc allows you to have certain items only appear when it's collecting doctests, so you can utilize doctest functionality without forcing the test to appear in docs, or to find an arbitrary private item to include it on.

When compiling a crate for use in doctests (with `--test` option), `rustdoc` will set `#[cfg(doctest)]`. Note that they will still link against only the public items of your crate; if you need to test private items, you need to write a unit test.

In this example, we're adding doctests that we know won't compile, to verify that our struct can only take in valid data:

    #![allow(unused)]
    fn main() {
    /// We have a struct here. Remember it doesn't accept negative numbers!
    pub struct MyStruct(pub usize);

    /// ```compile_fail
    /// let x = my_crate::MyStruct(-5);
    /// ```
    #[cfg(doctest)]
    pub struct MyStructOnlyTakesUsize;
    }

Note that the struct `MyStructOnlyTakesUsize` here isn't actually part of your public crate API. The use of `#[cfg(doctest)]` makes sure that this struct only exists while `rustdoc` is collecting doctests. This means that its doctest is executed when `--test` is passed to rustdoc, but is hidden from the public documentation.

Another possible use of `#[cfg(doctest)]` is to test doctests that are included in your README file without including it in your main documentation. For example, you could write this into your `lib.rs` to test your README as part of your doctests:

    #![feature(external_doc)]

    #[doc(include = "../README.md")]
    #[cfg(doctest)]
    pub struct ReadmeDoctests;

This will include your README as documentation on the hidden struct `ReadmeDoctests`, which will then be tested alongside the rest of your doctests.

# Linking to items by name

Rustdoc is capable of directly linking to other rustdoc pages using the path of the item as a link.

For example, in the following code all of the links will link to the rustdoc page for `Bar`:

    #![allow(unused)]
    fn main() {
    /// This struct is not [Bar]
    pub struct Foo1;

    /// This struct is also not [bar](Bar)
    pub struct Foo2;

    /// This struct is also not [bar][b]
    ///
    /// [b]: Bar
    pub struct Foo3;

    /// This struct is also not [`Bar`]
    pub struct Foo4;

    /// This struct *is* [`Bar`]!
    pub struct Bar;
    }

Backticks around the link will be stripped, so `[`Option`]` will correctly link to `Option`.

You can refer to anything in scope, and use paths, including `Self`, `self`, `super`, and `crate`. You may also use `foo()` and `foo!()` to refer to methods/functions and macros, respectively.

You can also refer to items with generic parameters like `Vec<T>`. The link will resolve as if you had written `[`Vec<T>`](Vec)`. Fully-qualified syntax (for example, `<Vec as IntoIterator>::into_iter()`) is not yet supported, however.

    #![allow(unused)]
    fn main() {
    use std::sync::mpsc::Receiver;

    /// This is a version of [`Receiver<T>`] with support for [`std::future`].
    ///
    /// You can obtain a [`std::future::Future`] by calling [`Self::recv()`].
    pub struct AsyncReceiver<T> {
        sender: Receiver<T>
    }

    impl<T> AsyncReceiver<T> {
        pub async fn recv() -> T {
            unimplemented!()
        }
    }
    }

You can also link to sections using URL fragment specifiers:

    #![allow(unused)]
    fn main() {
    /// This is a special implementation of [positional parameters].
    ///
    /// [positional parameters]: std::fmt#formatting-parameters
    struct MySpecialFormatter;
    }

Paths in Rust have three namespaces: type, value, and macro. Item names must be unique within their namespace, but can overlap with items outside of their namespace. In case of ambiguity, rustdoc will warn about the ambiguity and ask you to disambiguate, which can be done by using a prefix like `struct@`, `enum@`, `type@`, `trait@`, `union@`, `const@`, `static@`, `value@`, `fn@`, `function@`, `mod@`, `module@`, `method@`, `prim@`, `primitive@`, `macro@`, or `derive@`:

    #![allow(unused)]
    fn main() {
    /// See also: [`Foo`](struct@Foo)
    struct Bar;

    /// This is different from [`Foo`](fn@Foo)
    struct Foo {}

    fn Foo() {}
    }

You can also disambiguate for functions by adding `()` after the function name, or for macros by adding `!` after the macro name:

    #![allow(unused)]
    fn main() {
    /// See also: [`Foo`](struct@Foo)
    struct Bar;

    /// This is different from [`Foo()`]
    struct Foo {}

    fn Foo() {}
    }

Note: Because of how `macro_rules!` macros are scoped in Rust, the intra-doc links of a `macro_rules!` macro will be resolved relative to the crate root, as opposed to the module it is defined in.

# Lints

`rustdoc` provides lints to help you writing and testing your documentation. You can use them like any other lints by doing this:

    #![allow(missing_docs)] // allows the lint, no diagnostics will be reported
    #![warn(missing_docs)] // warn if there are missing docs
    #![deny(missing_docs)] // error if there are missing docs

Here is the list of the lints provided by `rustdoc`:

## broken_intra_doc_links

This lint warns by default. This lint detects when an intra-doc link fails to be resolved. For example:

    #![allow(unused)]
    fn main() {
    /// I want to link to [`Nonexistent`] but it doesn't exist!
    pub fn foo() {}
    }

You'll get a warning saying:

    warning: unresolved link to `Nonexistent`
     --> test.rs:1:24
      |
    1 | /// I want to link to [`Nonexistent`] but it doesn't exist!
      |                        ^^^^^^^^^^^^^ no item named `Nonexistent` in `test`

It will also warn when there is an ambiguity and suggest how to disambiguate:

    #![allow(unused)]
    fn main() {
    /// [`Foo`]
    pub fn function() {}

    pub enum Foo {}

    pub fn Foo(){}
    }

    warning: `Foo` is both an enum and a function
     --> test.rs:1:6
      |
    1 | /// [`Foo`]
      |      ^^^^^ ambiguous link
      |
      = note: `#[warn(broken_intra_doc_links)]` on by default
    help: to link to the enum, prefix with the item type
      |
    1 | /// [`enum@Foo`]
      |      ^^^^^^^^^^
    help: to link to the function, add parentheses
      |
    1 | /// [`Foo()`]
      |      ^^^^^^^

## private_intra_doc_links

This lint warns by default. This lint detects when intra-doc links from public to private items. For example:

    #![allow(unused)]
    fn main() {
    /// [private]
    pub fn public() {}
    fn private() {}
    }

This gives a warning that the link will be broken when it appears in your documentation:

    warning: public documentation for `public` links to private item `private`
     --> priv.rs:1:6
      |
    1 | /// [private]
      |      ^^^^^^^ this item is private
      |
      = note: `#[warn(private_intra_doc_links)]` on by default
      = note: this link will resolve properly if you pass `--document-private-items`

Note that this has different behavior depending on whether you pass `--document-private-items` or not! If you document private items, then it will still generate a link, despite the warning:

    warning: public documentation for `public` links to private item `private`
     --> priv.rs:1:6
      |
    1 | /// [private]
      |      ^^^^^^^ this item is private
      |
      = note: `#[warn(private_intra_doc_links)]` on by default
      = note: this link resolves only because you passed `--document-private-items`, but will break without

## missing_docs

This lint is allowed by default. It detects items missing documentation. For example:

    #![warn(missing_docs)]

    pub fn undocumented() {}
    fn main() {}

The `undocumented` function will then have the following warning:

    warning: missing documentation for a function
      --> your-crate/lib.rs:3:1
       |
     3 | pub fn undocumented() {}
       | ^^^^^^^^^^^^^^^^^^^^^

## missing_crate_level_docs

This lint is allowed by default. It detects if there is no documentation at the crate root. For example:

    #![allow(unused)]
    #![warn(missing_crate_level_docs)]
    fn main() {
    }

This will generate the following warning:

    warning: no documentation found for this crate's top-level module
      |
      = help: The following guide may be of use:
              https://doc.rust-lang.org/nightly/rustdoc/how-to-write-documentation.html

This is currently "allow" by default, but it is intended to make this a warning in the future. This is intended as a means to introduce new users on how to document their crate by pointing them to some instructions on how to get started, without providing overwhelming warnings like `missing_docs` might.

## missing_doc_code_examples

This lint is allowed by default and is nightly-only. It detects when a documentation block is missing a code example. For example:

    #![warn(missing_doc_code_examples)]

    /// There is no code example!
    pub fn no_code_example() {}
    fn main() {}

The `no_code_example` function will then have the following warning:

    warning: Missing code example in this documentation
      --> your-crate/lib.rs:3:1
       |
    LL | /// There is no code example!
       | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To fix the lint, you need to add a code example into the documentation block:

    #![allow(unused)]
    fn main() {
    /// There is no code example!
    ///
    /// ```
    /// println!("calling no_code_example...");
    /// no_code_example();
    /// println!("we called no_code_example!");
    /// ```
    pub fn no_code_example() {}
    }

## private_doc_tests

This lint is allowed by default. It detects documentation tests when they are on a private item. For example:

    #![warn(private_doc_tests)]

    mod foo {
        /// private doc test
        ///
        /// ```
        /// assert!(false);
        /// ```
        fn bar() {}
    }
    fn main() {}

Which will give:

    warning: Documentation test in private item
      --> your-crate/lib.rs:4:1
       |
     4 | /     /// private doc test
     5 | |     ///
     6 | |     /// ```
     7 | |     /// assert!(false);
     8 | |     /// ```
       | |___________^

## invalid_codeblock_attributes

This lint warns by default. It detects code block attributes in documentation examples that have potentially mis-typed values. For example:

    #![allow(unused)]
    fn main() {
    /// Example.
    ///
    /// ```should-panic
    /// assert_eq!(1, 2);
    /// ```
    pub fn foo() {}
    }

Which will give:

    warning: unknown attribute `should-panic`. Did you mean `should_panic`?
     --> src/lib.rs:1:1
      |
    1 | / /// Example.
    2 | | ///
    3 | | /// ```should-panic
    4 | | /// assert_eq!(1, 2);
    5 | | /// ```
      | |_______^
      |
      = note: `#[warn(invalid_codeblock_attributes)]` on by default
      = help: the code block will either not be tested if not marked as a rust one or won't fail if it doesn't panic when running

In the example above, the correct form is `should_panic`. This helps detect typo mistakes for some common attributes.

## invalid_html_tags

This lint is allowed by default and is nightly-only. It detects unclosed or invalid HTML tags. For example:

    #![allow(unused)]
    #![warn(invalid_html_tags)]

    fn main() {
    /// <h1>
    /// </script>
    pub fn foo() {}
    }

Which will give:

    warning: unopened HTML tag `script`
     --> foo.rs:1:1
      |
    1 | / /// <h1>
    2 | | /// </script>
      | |_____________^
      |
      = note: `#[warn(invalid_html_tags)]` on by default

    warning: unclosed HTML tag `h1`
     --> foo.rs:1:1
      |
    1 | / /// <h1>
    2 | | /// </script>
      | |_____________^

    warning: 2 warnings emitted

## non_autolinks

This lint is nightly-only and warns by default. It detects links which could use the "automatic" link syntax. For example:

    #![allow(unused)]
    fn main() {
    /// http://example.org
    /// [http://example.com](http://example.com)
    /// [http://example.net]
    ///
    /// [http://example.com]: http://example.com
    pub fn foo() {}
    }

Which will give:

    warning: this URL is not a hyperlink
     --> foo.rs:1:5
      |
    1 | /// http://example.org
      |     ^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://example.org>`
      |
      = note: `#[warn(non_autolinks)]` on by default

    warning: unneeded long form for URL
     --> foo.rs:2:5
      |
    2 | /// [http://example.com](http://example.com)
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://example.com>`

    warning: this URL is not a hyperlink
     --> foo.rs:3:6
      |
    3 | /// [http://example.net]
      |      ^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://example.net>`

# 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-priv-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.

# Advanced features

The features listed on this page fall outside the rest of the main categories.

## `#[cfg(doc)]`: Documenting platform-specific or feature-specific information

For conditional compilation, Rustdoc treats your crate the same way the compiler does. Only things from the host target are available (or from the given `--target` if present), and everything else is "filtered out" from the crate. This can cause problems if your crate is providing different things on different targets and you want your documentation to reflect all the available items you provide.

If you want to make sure an item is seen by Rustdoc regardless of what platform it's targeting, you can apply `#[cfg(doc)]` to it. Rustdoc sets this whenever it's building documentation, so anything that uses that flag will make it into documentation it generates. To apply this to an item with other `#[cfg]` filters on it, you can write something like `#[cfg(any(windows, doc))]`. This will preserve the item either when built normally on Windows, or when being documented anywhere.

Please note that this `cfg` is not passed to doctests.

Example:

    #![allow(unused)]
    fn main() {
    /// Token struct that can only be used on Windows.
    #[cfg(any(windows, doc))]
    pub struct WindowsToken;
    /// Token struct that can only be used on Unix.
    #[cfg(any(unix, doc))]
    pub struct UnixToken;
    }

Here, the respective tokens can only be used by dependent crates on their respective platforms, but they will both appear in documentation.

### Interactions between platform-specific docs

Rustdoc does not have a magic way to compile documentation 'as-if' you'd run it once for each platform (such a magic wand has been called the 'holy grail of rustdoc'). Instead, it sees all of your code at once, the same way the Rust compiler would if you passed it `--cfg doc`. However, Rustdoc has a trick up its sleeve to handle platform-specific code if it does receive it.

To document your crate, Rustdoc only needs to know the public signature of your functions. In particular, it doesn't have to know how any of your functions are implemented, so it ignores all type errors and name resolution errors with function bodies. Note that this does not work for anything outside a function body: since Rustdoc documents your types, it has to know what those types are! For example, this code will work regardless of the platform:

    pub fn f() {
        use std::os::windows::ffi::OsStrExt;
    }

but this will not, because the unknown type is part of the function signature:

    pub fn f() -> std::os::windows::ffi::EncodeWide<'static> {
        unimplemented!()
    }

For a more realistic example of code this allows, see the rustdoc test suite.

## Add aliases for an item in documentation search

This feature allows you to add alias(es) to an item when using the `rustdoc` search through the `doc(alias)` attribute. Example:

    #![allow(unused)]
    fn main() {
    #[doc(alias = "x")]
    #[doc(alias = "big")]
    pub struct BigX;
    }

Then, when looking for it through the `rustdoc` search, if you enter "x" or "big", search will show the `BigX` struct first.

There are some limitations on the doc alias names though: you can't use `"` or whitespace.

# Unstable features

Rustdoc is under active development, and like the Rust compiler, some features are only available on nightly releases. Some of these features are new and need some more testing before they're able to be released to the world at large, and some of them are tied to features in the Rust compiler that are unstable. Several features here require a matching `#![feature(...)]` attribute to enable, and thus are more fully documented in the Unstable Book. Those sections will link over there as necessary.

## Nightly-gated functionality

These features just require a nightly build to operate. Unlike the other features on this page, these don't need to be "turned on" with a command-line flag or a `#![feature(...)]` attribute in your crate. This can give them some subtle fallback modes when used on a stable release, so be careful!

### Error numbers for `compile-fail` doctests

As detailed in the chapter on documentation tests, you can add a `compile_fail` attribute to a doctest to state that the test should fail to compile. However, on nightly, you can optionally add an error number to state that a doctest should emit a specific error number:

    ```compile_fail,E0044
    extern { fn some_func<T>(x: T); }
    ```

This is used by the error index to ensure that the samples that correspond to a given error number properly emit that error code. However, these error codes aren't guaranteed to be the only thing that a piece of code emits from version to version, so this is unlikely to be stabilized in the future.

Attempting to use these error numbers on stable will result in the code sample being interpreted as plain text.

## Extensions to the `#[doc]` attribute

These features operate by extending the `#[doc]` attribute, and thus can be caught by the compiler and enabled with a `#![feature(...)]` attribute in your crate.

### `#[doc(cfg)]`: Recording what platforms or features are required for code to be present

You can use `#[doc(cfg(...))]` to tell Rustdoc exactly which platform items appear on. This has two effects:

1. doctests will only run on the appropriate platforms, and
2. When Rustdoc renders documentation for that item, it will be accompanied by a banner explaining that the item is only available on certain platforms.

`#[doc(cfg)]` is intended to be used alongside `#[cfg(doc)]`. For example, `#[cfg(any(windows, doc))]` will preserve the item either on Windows or during the documentation process. Then, adding a new attribute `#[doc(cfg(windows))]` will tell Rustdoc that the item is supposed to be used on Windows. For example:

    #![allow(unused)]
    #![feature(doc_cfg)]

    fn main() {
    /// Token struct that can only be used on Windows.
    #[cfg(any(windows, doc))]
    #[doc(cfg(windows))]
    pub struct WindowsToken;

    /// Token struct that can only be used on Unix.
    #[cfg(any(unix, doc))]
    #[doc(cfg(unix))]
    pub struct UnixToken;

    /// Token struct that is only available with the `serde` feature
    #[cfg(feature = "serde")]
    #[doc(cfg(feature = "serde"))]
    #[derive(serde::Deserialize)]
    pub struct SerdeToken;
    }

In this sample, the tokens will only appear on their respective platforms, but they will both appear in documentation.

`#[doc(cfg(...))]` was introduced to be used by the standard library and currently requires the `#![feature(doc_cfg)]` feature gate. For more information, see its chapter in the Unstable Book and its tracking issue.

### Adding your trait to the "Important Traits" dialog

Rustdoc keeps a list of a few traits that are believed to be "fundamental" to a given type when implemented on it. These traits are intended to be the primary interface for their types, and are often the only thing available to be documented on their types. For this reason, Rustdoc will track when a given type implements one of these traits and call special attention to it when a function returns one of these types. This is the "Important Traits" dialog, visible as a circle-i button next to the function, which, when clicked, shows the dialog.

In the standard library, the traits that qualify for inclusion are `Iterator`, `io::Read`, and `io::Write`. However, rather than being implemented as a hard-coded list, these traits have a special marker attribute on them: `#[doc(spotlight)]`. This means that you could apply this attribute to your own trait to include it in the "Important Traits" dialog in documentation.

The `#[doc(spotlight)]` attribute currently requires the `#![feature(doc_spotlight)]` feature gate. For more information, see its chapter in the Unstable Book and its tracking issue.

### Exclude certain dependencies from documentation

The standard library uses several dependencies which, in turn, use several types and traits from the standard library. In addition, there are several compiler-internal crates that are not considered to be part of the official standard library, and thus would be a distraction to include in documentation. It's not enough to exclude their crate documentation, since information about trait implementations appears on the pages for both the type and the trait, which can be in different crates!

To prevent internal types from being included in documentation, the standard library adds an attribute to their `extern crate` declarations: `#[doc(masked)]`. This causes Rustdoc to "mask out" types from these crates when building lists of trait implementations.

The `#[doc(masked)]` attribute is intended to be used internally, and requires the `#![feature(doc_masked)]` feature gate. For more information, see its chapter in the Unstable Book and its tracking issue.

### Include external files as API documentation

As designed in RFC 1990, Rustdoc can read an external file to use as a type's documentation. This is useful if certain documentation is so long that it would break the flow of reading the source. Instead of writing it all inline, writing `#[doc(include = "sometype.md")]` will ask Rustdoc to instead read that file and use it as if it were written inline.

`#[doc(include = "...")]` currently requires the `#![feature(external_doc)]` feature gate. For more information, see its chapter in the Unstable Book and its tracking issue.

## Unstable command-line arguments

These features are enabled by passing a command-line flag to Rustdoc, but the flags in question are themselves marked as unstable. To use any of these options, pass `-Z unstable-options` as well as the flag in question to Rustdoc on the command-line. To do this from Cargo, you can either use the `RUSTDOCFLAGS` environment variable or the `cargo rustdoc` command.

### `--markdown-before-content`: include rendered Markdown before the content

Using this flag looks like this:

    $ rustdoc src/lib.rs -Z unstable-options --markdown-before-content extra.md
    $ rustdoc README.md -Z unstable-options --markdown-before-content extra.md

Just like `--html-before-content`, this allows you to insert extra content inside the `<body>` tag but before the other content `rustdoc` would normally produce in the rendered documentation. However, instead of directly inserting the file verbatim, `rustdoc` will pass the files through a Markdown renderer before inserting the result into the file.

### `--markdown-after-content`: include rendered Markdown after the content

Using this flag looks like this:

    $ rustdoc src/lib.rs -Z unstable-options --markdown-after-content extra.md
    $ rustdoc README.md -Z unstable-options --markdown-after-content extra.md

Just like `--html-after-content`, this allows you to insert extra content before the `</body>` tag but after the other content `rustdoc` would normally produce in the rendered documentation. However, instead of directly inserting the file verbatim, `rustdoc` will pass the files through a Markdown renderer before inserting the result into the file.

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

Using this flag looks like this:

    $ rustdoc src/lib.rs -Z unstable-options --playground-url https://play.rust-lang.org/

When rendering a crate's docs, this flag gives the base URL of the Rust Playground, to use for generating `Run` buttons. Unlike `--markdown-playground-url`, this argument works for standalone Markdown files and Rust crates. This works the same way as adding `#![doc(html_playground_url = "url")]` to your crate root, as mentioned in the chapter about the `#[doc]` attribute. Please be aware that the official Rust Playground at https://play.rust-lang.org does not have every crate available, so if your examples require your crate, make sure the playground you provide has your crate available.

If both `--playground-url` and `--markdown-playground-url` are present when rendering a standalone Markdown file, the URL given to `--markdown-playground-url` will take precedence. If both `--playground-url` and `#![doc(html_playground_url = "url")]` are present when rendering crate docs, the attribute will take precedence.

### `--sort-modules-by-appearance`: control how items on module pages are sorted

Using this flag looks like this:

    $ rustdoc src/lib.rs -Z unstable-options --sort-modules-by-appearance

Ordinarily, when `rustdoc` prints items in module pages, it will sort them alphabetically (taking some consideration for their stability, and names that end in a number). Giving this flag to `rustdoc` will disable this sorting and instead make it print the items in the order they appear in the source.

### `--resource-suffix`: modifying the name of CSS/JavaScript in crate docs

Using this flag looks like this:

    $ rustdoc src/lib.rs -Z unstable-options --resource-suffix suf

When rendering docs, `rustdoc` creates several CSS and JavaScript files as part of the output. Since all these files are linked from every page, changing where they are can be cumbersome if you need to specially cache them. This flag will rename all these files in the output to include the suffix in the filename. For example, `light.css` would become `light-suf.css` with the above command.

### `--display-warnings`: display warnings when documenting or running documentation tests

Using this flag looks like this:

    $ rustdoc src/lib.rs -Z unstable-options --display-warnings
    $ rustdoc --test src/lib.rs -Z unstable-options --display-warnings

The intent behind this flag is to allow the user to see warnings that occur within their library or their documentation tests, which are usually suppressed. However, due to a bug, this flag doesn't 100% work as intended. See the linked issue for details.

### `--extern-html-root-url`: control how rustdoc links to non-local crates

Using this flag looks like this:

    $ rustdoc src/lib.rs -Z unstable-options --extern-html-root-url some-crate=https://example.com/some-crate/1.0.1

Ordinarily, when rustdoc wants to link to a type from a different crate, it looks in two places: docs that already exist in the output directory, or the `#![doc(doc_html_root)]` set in the other crate. However, if you want to link to docs that exist in neither of those places, you can use these flags to control that behavior. When the `--extern-html-root-url` flag is given with a name matching one of your dependencies, rustdoc use that URL for those docs. Keep in mind that if those docs exist in the output directory, those local docs will still override this flag.

### `-Z force-unstable-if-unmarked`

Using this flag looks like this:

    $ rustdoc src/lib.rs -Z force-unstable-if-unmarked

This is an internal flag intended for the standard library and compiler that applies an `#[unstable]` attribute to any dependent crate that doesn't have another stability attribute. This allows `rustdoc` to be able to generate documentation for the compiler crates and the standard library, as an equivalent command-line argument is provided to `rustc` when building those crates.

### `--index-page`: provide a top-level landing page for docs

This feature allows you to generate an index-page with a given markdown file. A good example of it is the rust documentation index.

With this, you'll have a page which you can custom as much as you want at the top of your crates.

Using `index-page` option enables `enable-index-page` option as well.

### `--enable-index-page`: generate a default index page for docs

This feature allows the generation of a default index-page which lists the generated crates.

### `--static-root-path`: control how static files are loaded in HTML output

Using this flag looks like this:

    $ rustdoc src/lib.rs -Z unstable-options --static-root-path '/cache/'

This flag controls how rustdoc links to its static files on HTML pages. If you're hosting a lot of crates' docs generated by the same version of rustdoc, you can use this flag to cache rustdoc's CSS, JavaScript, and font files in a single location, rather than duplicating it once per "doc root" (grouping of crate docs generated into the same output directory, like with `cargo doc`). Per-crate files like the search index will still load from the documentation root, but anything that gets renamed with `--resource-suffix` will load from the given path.

### `--persist-doctests`: persist doctest executables after running

Using this flag looks like this:

    $ rustdoc src/lib.rs --test -Z unstable-options --persist-doctests target/rustdoctest

This flag allows you to keep doctest executables around after they're compiled or run. Usually, rustdoc will immediately discard a compiled doctest after it's been tested, but with this option, you can keep those binaries around for farther testing.

### `--show-coverage`: calculate the percentage of items with documentation

Using this flag looks like this:

    $ rustdoc src/lib.rs -Z unstable-options --show-coverage

If you want to determine how many items in your crate are documented, pass this flag to rustdoc. When it receives this flag, it will count the public items in your crate that have documentation, and print out the counts and a percentage instead of generating docs.

Some methodology notes about what rustdoc counts in this metric:

- Rustdoc will only count items from your crate (i.e. items re-exported from other crates don't count).
- Docs written directly onto inherent impl blocks are not counted, even though their doc comments are displayed, because the common pattern in Rust code is to write all inherent methods into the same impl block.
- Items in a trait implementation are not counted, as those impls will inherit any docs from the trait itself.
- By default, only public items are counted. To count private items as well, pass `--document-private-items` at the same time.

Public items that are not documented can be seen with the built-in `missing_docs` lint. Private items that are not documented can be seen with Clippy's `missing_docs_in_private_items` lint.

### `--enable-per-target-ignores`: allow `ignore-foo` style filters for doctests

Using this flag looks like this:

    $ rustdoc src/lib.rs -Z unstable-options --enable-per-target-ignores

This flag allows you to tag doctests with compiletest style `ignore-foo` filters that prevent rustdoc from running that test if the target triple string contains foo. For example:

    #![allow(unused)]
    fn main() {
    ///```ignore-foo,ignore-bar
    ///assert!(2 == 2);
    ///```
    struct Foo;
    }

This will not be run when the build target is `super-awesome-foo` or `less-bar-awesome`. If the flag is not enabled, then rustdoc will consume the filter, but do nothing with it, and the above example will be run for all targets. If you want to preserve backwards compatibility for older versions of rustdoc, you can use

    #![allow(unused)]
    fn main() {
    ///```ignore,ignore-foo
    ///assert!(2 == 2);
    ///```
    struct Foo;
    }

In older versions, this will be ignored on all targets, but on newer versions `ignore-gnu` will override `ignore`.

### `--runtool`, `--runtool-arg`: program to run tests with; args to pass to it

Using these options looks like this:

    $ rustdoc src/lib.rs -Z unstable-options --runtool runner --runtool-arg --do-thing --runtool-arg --do-other-thing

These options can be used to run the doctest under a program, and also pass arguments to that program. For example, if you want to run your doctests under valgrind you might run

    $ rustdoc src/lib.rs -Z unstable-options --runtool valgrind

Another use case would be to run a test inside an emulator, or through a Virtual Machine.

### `--show-coverage`: get statistics about code documentation coverage

This option allows you to get a nice overview over your code documentation coverage, including both doc-comments and code examples in the doc-comments. Example:

    $ rustdoc src/lib.rs -Z unstable-options --show-coverage
    +-------------------------------------+------------+------------+------------+------------+
    | File                                | Documented | Percentage |   Examples | Percentage |
    +-------------------------------------+------------+------------+------------+------------+
    | lib.rs                              |          4 |     100.0% |          1 |      25.0% |
    +-------------------------------------+------------+------------+------------+------------+
    | Total                               |          4 |     100.0% |          1 |      25.0% |
    +-------------------------------------+------------+------------+------------+------------+

You can also use this option with the `--output-format` one:

    $ rustdoc src/lib.rs -Z unstable-options --show-coverage --output-format json
    {"lib.rs":{"total":4,"with_docs":4,"total_examples":4,"with_examples":1}}

Calculating code examples follows these rules:

1. These items aren't accounted by default:

- struct/union field
- enum variant
- constant
- static
- typedef

2. If one of the previously listed items has a code example, then it'll be counted.

# References

There are many great `rustdoc` references out there. If you know of other great resources, please submit a pull request!

## Official

- Learn Rust
- Rust By Example
- Rust Reference
- RFC 1574: More API Documentation Conventions
- RFC 1946: Intra Rustdoc Links

## Community

- API Guidelines
- Github tagged RFCs
- Github tagged issues
- RFC (stalled) front page styleguide
- Guide on how to write documenation for a Rust crate