CARGO-DOC(1)

πŸ“¦ version 1.82.0, in ubuntu jammy, ubuntu noble

πŸ’– CARGO-DOC(1) πŸ’–

okay bestie, pull up a chair, we're doing cargo-doc β€” the manpage is literally called CARGO-DOC(1) and it's here to Build a package's documentation. no cap. πŸŽ€


🌸 NAME 🌸

cargo-doc β€” Build a package's documentation


✨ SYNOPSIS ✨

cargo doc [options]

πŸ¦‹ DESCRIPTION πŸ¦‹

So basically cargo doc builds the docs for your local package plus all its dependencies, and drops the whole thing in target/doc in rustdoc's usual format. that's it, that's the vibe. πŸ’…


πŸ‘‘ OPTIONS πŸ‘‘

πŸ“š Documentation Options πŸ“š

  • --open β€” Opens the docs in a browser once they're built. It'll use your default browser unless you set a different 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 β€” Don't build docs for dependencies. just yours, main character energy only.
  • --document-private-items β€” Include non-public items in the documentation. This gets enabled by default if you're documenting a binary target. ✨

🎯 Package Selection 🎯

By default, when you don't give any package selection options, which packages get picked depends on the selected manifest file (based on the current working directory, unless --manifest-path is given). If the manifest is the root of a workspace, then the workspace's default members get selected. Otherwise, only the package defined by that manifest gets selected.

The default members of a workspace can be set explicitly with the workspace.default-members key in the root manifest. If that's 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. Check out cargo-pkgid(1) for the SPEC format. This flag can be given multiple times and supports common Unix glob patterns like *, ? and []. BUT β€” and this is the part everyone fumbles β€” to stop your shell from accidentally expanding those glob patterns before Cargo even gets to handle them, you must wrap each pattern in single quotes or double quotes. fr fr. πŸ™ˆ
  • --workspace β€” Document all members in the workspace. the whole squad. 🫢
  • --all β€” Deprecated alias for --workspace. it's giving old news, use the other one.
  • --exclude *SPEC*… β€” Exclude the specified packages. Must be used together with the --workspace flag. This flag can be given multiple times and supports common Unix glob patterns like *, ? and []. And same as before: wrap each pattern in single quotes or double quotes so your shell doesn't jump the gun and expand them before Cargo handles them.

🧚 Target Selection 🧚

When you don't give any target selection options, cargo doc will document all binary and library targets of the selected package. The binary gets skipped if its name is the same as the lib target. Binaries also get 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.

  • --lib β€” Document the package's library. πŸ“–
  • --bin *name*… β€” Document the specified binary. This flag can be given multiple times and supports common Unix glob patterns.
  • --bins β€” Document all binary targets.
  • --example *name*… β€” Document the specified example. This flag can be given multiple times and supports common Unix glob patterns.
  • --examples β€” Document all example targets.

🌈 Feature Selection 🌈

The feature flags let you 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. iykyk.

  • -F features, --features features β€” Space or comma separated list of features to activate. Features of workspace members can be enabled with the package-name/feature-name syntax. This flag can be given multiple times, and giving it multiple times enables all the specified features.
  • --all-features β€” Activate every available feature of all selected packages. go big or go home. πŸ”₯
  • --no-default-features β€” Don't activate the default feature of the selected packages.

βš™οΈ Compilation Options βš™οΈ

  • --target triple β€” Document for the given architecture. The default is the host architecture. The general format of the triple is <arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for a list of supported targets. This flag can be given multiple times.

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

Heads up bestie: specifying this flag makes Cargo run in a different mode where the target artifacts get placed in a separate directory. See the build cache documentation (https://doc.rust-lang.org/cargo/guide/build-cache.html) for more details. πŸ‘€
  • -r, --release β€” Document optimized artifacts with the release profile. See also the --profile option if you wanna pick 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=fmts β€” Outputs info on how long each compilation takes, and tracks concurrency information over time. Accepts an optional comma-separated list of output formats; --timings with no argument will default to --timings=html. Specifying an output format (instead of the default) is unstable and requires -Zunstable-options. Valid output formats:
    • html (unstable, requires -Zunstable-options): Writes a human-readable file cargo-timing.html to the target/cargo-timings directory with a report of the compilation. It also writes a report to the same directory with a timestamp in the filename, in case you wanna look back at older runs. HTML output is suitable for human consumption only, and does not provide machine-readable timing data.
    • json (unstable, requires -Zunstable-options): Emits machine-readable JSON information about timing information.

πŸ“¦ 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. Can be given twice for "very verbose" output, which includes extra stuff like 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). shhh. 🀫
  • --color when β€” Controls when colored output is used. Valid values:
    • auto (default): Automatically detect if color support is available on the terminal.
    • always: Always display colors.
    • never: Never display colors.

May also be specified with the term.color config value (https://doc.rust-lang.org/cargo/reference/config.html).

  • --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, so it respects rustc's default color scheme. Cannot be used with human or short.
    • json-render-diagnostics: Tells Cargo to not include rustc diagnostics in the JSON messages it prints, and instead have Cargo itself render the JSON diagnostics coming from rustc. Cargo's own JSON diagnostics, plus 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 the rust-version specification in packages. pretend you didn't see it. πŸ™ˆ
  • --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 these scenarios happens:
    • 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, like 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 isn't available. With this flag, Cargo will attempt to proceed without the network if it can.
Careful bestie β€” this may result in a 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. two for one, slay.
  • --lockfile-path PATH β€” Changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock (e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that providing --lockfile-path will ignore the existing lockfile at the default path, and instead will either use the lockfile from PATH, or write a new lockfile into the provided PATH if it doesn't exist. This flag can be used to run most commands in read-only directories, writing the lockfile into the provided PATH. ✨

This one's only a thing on the nightly channel <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html>, bestie, and it needs the -Z unstable-options flag to even turn on (peep #14421 <https://github.com/rust-lang/cargo/issues/14421>). πŸ’…


✨ Common Options ✨

  • +toolchain β€” if you installed Cargo through rustup and the very first thing you type after cargo starts with +, it gets read as a rustup toolchain name (think +stable or +nightly). Wanna know how toolchain overrides actually work? The rustup documentation <https://rust-lang.github.io/rustup/overrides.html> has you covered. πŸ¦‹
  • --config KEY=VALUE or PATH β€” overrides a Cargo config value, no big deal. The argument is either TOML syntax in KEY=VALUE form, or a path pointing at an extra config file. You can drop this flag as many times as you want. More deets live in the command-line overrides section <https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>.
  • -C PATH β€” switches up the current working directory before anything you asked for actually runs. So yeah, it changes where Cargo looks by default for the project manifest (Cargo.toml), and which directories get searched when discovering .cargo/config.toml, for example. Heads up though: this option has to come before the command name, like cargo -C path/to/my-project build.
Okay so this one is nightly-only πŸŒ™ β€” it lives on the nightly channel <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and you need the -Z unstable-options flag to enable it (see #10098 <https://github.com/rust-lang/cargo/issues/10098>).
  • -h, --help β€” prints help information. The classic. πŸ™ˆ
  • -Z flag β€” unstable (nightly-only) flags for Cargo. Run cargo -Z help for the details.

🌈 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>. Defaults to the number of logical CPUs. If it's negative, it sets the max number of parallel jobs to the number of logical CPUs plus whatever value you gave. If you hand it a string default, it sets the value back to defaults. And it should not be 0. Like, ever. πŸ’€
  • --keep-going β€” builds as many crates in the dependency graph as it possibly can, instead of bailing on the very first one that fails to build.

For example, say the current package depends on two dependencies, fails and works, and one of them refuses to build. Then cargo doc -j1 may or may not build the one that succeeds (it 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 runs first fails. Determination. We love to see it. πŸ‘‘


🌸 ENVIRONMENT 🌸

Cargo reads some environment variables, and the reference <https://doc.rust-lang.org/cargo/reference/environment-variables.html> has all the details on that. Go look, it's worth it fr fr.


✨ EXIT STATUS ✨

  1. 0: Cargo succeeded. πŸŽ‰
  2. 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 the whole page, bestie!! This one's from Cargo's CARGO-DOC(1) β€” that source/version-and-date footer energy, you know the vibe β€” so if you ever forget any of it, the manpage is right there waiting for you. Stay iconic, keep your -j1 and your --keep-going close, and go doc some crates. πŸ’œβœ¨

πŸ“¦ where this page comes from