LinuxCommandLibrary

cargo-doc

Build and view Rust crate documentation

TLDR

Build the documentation for the current project and all dependencies

$ cargo [[d|doc]]
copy

Do not build documentation for dependencies
$ cargo [[d|doc]] --no-deps
copy

Build and open the documentation in a browser
$ cargo [[d|doc]] --open
copy

Build and view the documentation of a particular package
$ cargo [[d|doc]] --open [[-p|--package]] [package]
copy

SYNOPSIS

cargo doc [OPTIONS] [[--] <PATH>]

PARAMETERS

--open
    Builds docs and opens them in the default web browser.

--no-deps
    Do not build documentation for dependencies.

--lib
    Document only the library, not binaries/examples/tests.

--bins
    Document all binaries.

--examples
    Document all example targets.

--tests
    Document all test targets.

--benches
    Document all benchmark targets.

--all
    Document all packages in the workspace.

--target <TRIPLE>
    Target triple to document for (e.g., x86_64-unknown-linux-gnu).

--release
    Document in release mode (optimized).

--all-features
    Document with all features enabled.

--no-default-features
    Document without default features.

-p SPEC …
    Package(s) to document (e.g., my-crate).

--workspace
    Document all packages in workspace.

-v …
    Verbosity level (-v, -vv, -vvv for more output).

--color <WHEN>
    Control colored output (always, auto, never).

--frozen / --locked
    Do not update lock file or Cargo.lock.

DESCRIPTION

The cargo doc command builds documentation for a Rust crate and its dependencies using the rustdoc tool. It generates HTML documentation by default, placed in the target/doc directory as target/doc/<crate-name>/index.html.

Cargo doc processes the crate's source code, including libraries, binaries, examples, tests, and benchmarks if specified. It supports conditional compilation via Cargo features, allowing documentation for specific configurations. Documentation includes API references, module overviews, search functionality, and source code links.

Common use cases include local development for reviewing APIs, preparing releases, or integrating with CI/CD pipelines. It automatically handles dependencies unless --no-deps is used, ensuring complete docs. Run cargo doc --open to build and immediately view in a web browser.

This command respects Cargo.toml metadata like doc-url and workspace settings. It fails if documentation comments are malformed or if rustdoc encounters errors.

CAVEATS

Requires rustdoc toolchain; fails on syntax errors in docs. Large workspaces may consume significant memory/disk space. Documentation not built for proc-macros by default.

OUTPUT LOCATION

Docs saved to target/doc/<pkg>/index.html; use CARGO_TARGET_DIR env var to override.

EXAMPLE USAGE

cargo doc --lib --open
Documents library and opens browser.

cargo doc -p mylib --no-deps
Documents specific package without deps.

HISTORY

Introduced in Cargo 0.0.1 (2014) as part of initial Rust package manager from Mozilla Research. Evolved with Rust editions; major enhancements in Cargo 0.20+ for workspace support and --open. Maintained by Rust Project since 2015.

SEE ALSO

rustdoc(1), cargo build(1), cargo test(1), cargo check(1)

Copied to clipboard