CARGO-DOC(1)

๐Ÿ’– CARGO-DOC(1) ๐Ÿ’– โ€” the manual, bestie! โœจ

okay so like, this is the manual page for cargo doc, and it's literally everything you need to know to build docs without losing your mind ๐Ÿ’…

โœจ NAME โœจ

cargo-doc โ€” Build a packageโ€™s documentation ๐Ÿฆ‹

๐ŸŒธ SYNOPSIS ๐ŸŒธ

cargo doc [options]

๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

okay bestie, so cargo doc builds the documentation for your local package and all its dependencies. The output goes into target/doc in rustdocโ€™s usual format. no cap, it's that simple โœจ

Note: Documentation generation is cumulative: existing doc files in the target directory are preserved across different cargo doc invocations. To remove existing generated docs, pass --doc to cargo-clean(1). ๐Ÿ’€ don't say I didn't warn you.

๐Ÿ’… OPTIONS ๐Ÿ’…

๐Ÿ“š Documentation Options ๐Ÿ“š

  • --open โ€” Open the docs in a browser after building them. This will use your default browser unless you define another one in the BROWSER environment variable or use the doc.browser configuration option (https://doc.rust-lang.org/cargo/reference/config.html#docbrowser). ๐ŸŽ€
  • --no-deps โ€” Do not build documentation for dependencies. bye felicia ๐Ÿ‘‹
  • --document-private-items โ€” Include non-public items in the documentation. This will be enabled by default if documenting a binary target. ๐Ÿ”ฅ

๐Ÿ“ฆ Package Selection ๐Ÿ“ฆ

By default, when no package selection options are given, the packages selected depend on the selected manifest file (based on the current working directory if --manifest-path is not given). If the manifest is the root of a workspace then the workspaces default members are selected, otherwise only the package defined by the manifest will be selected.

The default members of a workspace can be set explicitly with the workspace.default-members key in the root manifest. If this is not set, a virtual workspace will include all workspace members (equivalent to passing --workspace), and a non-virtual workspace will include only the root crate itself. ๐Ÿ’–

  • -p specโ€ฆ, --package specโ€ฆ โ€” Document only the specified packages. See cargo-pkgid(1) for the SPEC format. This flag may be specified multiple times and supports common Unix glob patterns like *, ? and []. However, to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must use single quotes or double quotes around each pattern. ๐Ÿš
  • --workspace โ€” Document all members in the workspace. ๐Ÿ‘‘
  • --all โ€” Deprecated alias for --workspace. ๐Ÿ’€
  • --exclude SPECโ€ฆ โ€” Exclude the specified packages. Must be used in conjunction with the --workspace flag. This flag may be specified multiple times and supports common Unix glob patterns like *, ? and []. However, to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must use single quotes or double quotes around each pattern. ๐Ÿ™ˆ

๐ŸŽฏ Target Selection ๐ŸŽฏ

When no target selection options are given, cargo doc will document all binary and library targets of the selected package. The binary will be skipped if its name is the same as the lib target. Binaries are skipped if they have required-features that are missing. ๐Ÿ˜ญ

The default behavior can be changed by setting doc = false for the target in the manifest settings. Using target selection options will ignore the doc flag and will always document the given target. periodt ๐Ÿ’…

  • --lib โ€” Document the packageโ€™s library. ๐Ÿ“š
  • --bin nameโ€ฆ โ€” Document the specified binary. This flag may be specified multiple times and supports common Unix glob patterns. ๐Ÿฆ‹
  • --bins โ€” Document all binary targets. โœจ
  • --example nameโ€ฆ โ€” Document the specified example. This flag may be specified multiple times and supports common Unix glob patterns. ๐ŸŒธ
  • --examples โ€” Document all example targets. ๐ŸŽ€

โœจ Feature Selection โœจ

The feature flags allow you to control which features are enabled. When no feature options are given, the default feature is activated for every selected package. ๐Ÿ’–

See the features documentation (https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options) for more details. ๐Ÿ”

  • -F features, --features features โ€” Space or comma separated list of features to activate. Features of workspace members may be enabled with package-name/feature-name syntax. This flag may be specified multiple times, which enables all specified features. ๐Ÿ”ฅ
  • --all-features โ€” Activate all available features of all selected packages. ๐Ÿ‘‘
  • --no-default-features โ€” Do not activate the default feature of the selected packages. ๐Ÿ™…โ€โ™€๏ธ

โš™๏ธ Compilation Options โš™๏ธ

  • --target triple โ€” Document for the specified target architecture. Flag may be specified multiple times. The default is the host architecture. The general format of the triple is <arch><sub>-<vendor>-<sys>-<abi>. ๐ŸŒ

Possible values:

  • Any supported target in rustc --print target-list. ๐Ÿ“‹
  • "host-tuple", which will internally be substituted by the hostโ€™s target. This can be particularly useful if youโ€™re cross-compiling some crates, and donโ€™t want to specify your hostโ€™s machine as a target (for instance, an xtask in a shared project that may be worked on by many hosts). ๐Ÿ’ป
  • A path to a custom target specification. See Custom Target Lookup Path (https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path) for more information. ๐Ÿ—บ๏ธ

This may also be specified with the build.target config value (https://doc.rust-lang.org/cargo/reference/config.html). โš™๏ธ

Note that specifying this flag makes Cargo run in a different mode where the target artifacts are placed in a separate directory. See the build cache (https://doc.rust-lang.org/cargo/reference/build-cache.html) documentation for more details. ๐Ÿ“ฆ

  • -r, --release โ€” Document optimized artifacts with the release profile. See also the --profile option for choosing a specific profile by name. ๐Ÿš€
  • --profile name โ€” Document with the given profile. See the reference (https://doc.rust-lang.org/cargo/reference/profiles.html) for more details on profiles. ๐ŸŽ›๏ธ
  • --timings โ€” Output information how long each compilation takes, and track concurrency information over time. โฑ๏ธ

A file cargo-timing.html will be written to the target/cargo-timings directory at the end of the build. An additional report with a timestamp in its filename is also written if you want to look at a previous run. These reports are suitable for human consumption only, and do not provide machine-readable timing data. ๐Ÿ“Š

๐Ÿ“ Output Options ๐Ÿ“

  • --target-dir directory โ€” Directory for all generated artifacts and intermediate files. May also be specified with the CARGO_TARGET_DIR environment variable, or the build.target-dir config value (https://doc.rust-lang.org/cargo/reference/config.html). Defaults to target in the root of the workspace. ๐Ÿ—‚๏ธ

๐Ÿ–ฅ๏ธ Display Options ๐Ÿ–ฅ๏ธ

  • -v, --verbose โ€” Use verbose output. May be specified twice for โ€œvery verboseโ€ output which includes extra output such as dependency warnings and build script output. May also be specified with the term.verbose config value (https://doc.rust-lang.org/cargo/reference/config.html). ๐Ÿ—ฃ๏ธ
  • -q, --quiet โ€” Do not print cargo log messages. May also be specified with the term.quiet config value (https://doc.rust-lang.org/cargo/reference/config.html). ๐Ÿคซ
  • --color when โ€” Control when colored output is used. Valid values: ๐ŸŽจ
  • --message-format fmt โ€” The output format for diagnostic messages. Can be specified multiple times and consists of comma-separated values. Valid values: ๐Ÿ“จ
    • human (default): Display in a human-readable text format. Conflicts with short and json.
    • short: Emit shorter, human-readable text messages. Conflicts with human and json.
    • json: Emit JSON messages to stdout. See the reference (https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages) for more details. Conflicts with human and short.
    • json-diagnostic-short: Ensure the rendered field of JSON messages contains the โ€œshortโ€ rendering from rustc. Cannot be used with human or short.
    • json-diagnostic-rendered-ansi: Ensure the rendered field of JSON messages contains embedded ANSI color codes for respecting rustcโ€™s default color scheme. Cannot be used with human or short.
    • json-render-diagnostics: Instruct Cargo to not include rustc diagnostics in JSON messages printed, but instead Cargo itself should render the JSON diagnostics coming from rustc. Cargoโ€™s own JSON diagnostics and others coming from rustc are still emitted. Cannot be used with human or short.

๐Ÿ“œ Manifest Options ๐Ÿ“œ

  • --manifest-path path โ€” Path to the Cargo.toml file. By default, Cargo searches for the Cargo.toml file in the current directory or any parent directory. ๐Ÿ”
  • --ignore-rust-version โ€” Ignore rust-version specification in packages. ๐Ÿ™ˆ
  • --locked โ€” Asserts that the exact same dependencies and versions are used as when the existing Cargo.lock file was originally generated. Cargo will exit with an error when either of the following scenarios arises: ๐Ÿ”’
    • The lock file is missing.
    • Cargo attempted to change the lock file due to a different dependency resolution. It may be used in environments where deterministic builds are desired, such as in CI pipelines. ๐Ÿ—๏ธ
  • --offline โ€” Prevents Cargo from accessing the network for any reason. Without this flag, Cargo will stop with an error if it needs to access the network and the network is not available. With this flag, Cargo will attempt to proceed without the network if possible. โœˆ๏ธ

Beware that this may result in different dependency resolution than online mode. Cargo will restrict itself to crates that are downloaded locally, even if there might be a newer version as indicated in the local copy of the index. See the cargo-fetch(1) command to download dependencies before going offline. ๐Ÿ’พ

May also be specified with the net.offline config value (https://doc.rust-lang.org/cargo/reference/config.html). ๐ŸŒ

  • --frozen โ€” Equivalent to specifying both --locked and --offline. โ„๏ธ

โœจ Common Options โœจ

  • +toolchain โ€” bestie, if Cargo got installed through rustup and the very first argument you hand to cargo starts with a +, Cargo treats that whole thing as a rustup toolchain name (think +stable or +nightly). Wanna know how toolchain overrides actually work? Go read the rustup docs, they got you: https://rust-lang.github.io/rustup/overrides.html ๐Ÿ’–
  • --config KEY=VALUE or PATH โ€” this one overrides a Cargo configuration value, no cap. The argument is either TOML syntax in the form KEY=VALUE, or a path pointing to an extra configuration file. You can pass this flag as many times as you want, it's not a one-and-done situation. More deets live in the command-line overrides section: https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides โœจ
  • -C PATH โ€” changes the current working directory before it runs any of the operations you asked for. So it messes with stuff like where Cargo goes looking for the project manifest (Cargo.toml) by default, and also which directories get searched when discovering .cargo/config.toml, for example. โš ๏ธ Important: this option has to show up before the command name, like this:
cargo -C path/to/my-project build

Also it's only a thing on the nightly channel (https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and it needs the -Z unstable-options flag to switch on (peep issue #10098: https://github.com/rust-lang/cargo/issues/10098). ๐Ÿ™ˆ

  • -h, --help โ€” prints help information. Your lil "what do I even do" button. ๐Ÿซถ
  • -Z flag โ€” these are the unstable, nightly-only flags for Cargo. Curious what's in there? Run cargo -Z help and it'll spill everything. ๐Ÿ”ฅ

๐ŸŽ€ Miscellaneous Options ๐ŸŽ€

  • -j N, --jobs N โ€” how many parallel jobs to run. You can also set this with the build.jobs config value (https://doc.rust-lang.org/cargo/reference/config.html). Left alone, it defaults to the number of logical CPUs. If you give it a negative value, it sets the maximum number of parallel jobs to the number of logical CPUs plus whatever value you provided. If you pass the string default, it sets the value back to defaults. And listen โ€” it should not be 0. Don't do it. ๐Ÿ’€
  • --keep-going โ€” build as many crates in the dependency graph as it possibly can, instead of aborting the whole build the moment one of them fails to build. It's giving "we don't stop for nobody." ๐Ÿ‘‘

For example, say the current package depends on dependencies fails and works, and one of them fails to build. In that case cargo doc -j1 may or may not build the one that succeeds (it all depends on which of the two builds Cargo picked to run first), whereas cargo doc -j1 --keep-going would definitely run both builds, even if the one it ran first fails.

This option is only available on the nightly channel (https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and it requires the -Z unstable-options flag to enable. ๐ŸŒ™


๐ŸŒ ENVIRONMENT ๐ŸŒ

Cargo reads environment variables, yes it does โ€” and the reference has all the details on exactly which ones: https://doc.rust-lang.org/cargo/reference/environment-variables.html ๐Ÿ’œ


๐Ÿšฆ EXIT STATUS ๐Ÿšฆ

  • 0: Cargo succeeded. Slay. ๐ŸŽ‰
  • 101: Cargo failed to complete. ๐Ÿ˜ญ

๐ŸŒˆ EXAMPLES ๐ŸŒˆ

  1. Build the local package documentation and its dependencies and output to target/doc.
cargo doc

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

cargo(1), cargo-rustdoc(1), rustdoc(1)


and that's a wrap on the CARGO-DOC(1) page, bestie!! ๐Ÿซถโœจ you just read the whole thing, and yeah it's CARGO-DOC(1) โ€” go forth and document your crates, you absolute icon. ๐Ÿ’…๐ŸŽ€๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from