CARGO-DOC(1)

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

okay so bestie, sit down, hydrate, because today we're reading the General Commands Manual page for cargo-doc and it's honestly iconic πŸ’…βœ¨


πŸ’– NAME πŸ’–

cargo-doc β€” build a package's documentation πŸ“š


πŸ¦‹ SYNOPSIS πŸ¦‹

cargo doc [options]

🌸 DESCRIPTION 🌸

So what this does is: it builds the documentation for the local package and all of its dependencies. The output gets dropped into target/doc in rustdoc's usual format. That's it, that's the whole vibe. no cap. πŸŽ€


✨ OPTIONS ✨

πŸ“– Documentation Options πŸ“–

  • --open β€” bestie, this one literally opens the docs in a browser for you once they're built 🫢. It'll use your default browser unless you've defined a different one in the BROWSER environment variable, or unless you've set the doc.browser config option (https://doc.rust-lang.org/cargo/reference/config.html#docbrowser).
  • --no-deps β€” do not build docs for your dependencies. just you. main character energy only πŸ‘‘
  • --document-private-items β€” this one includes non-public items in the documentation, so all the little private secrets get documented too πŸ™ˆ. Heads up: this is enabled by default if you're documenting a binary target.

πŸ“¦ Package Selection πŸ“¦

Okay so default behaviour, when you pass zero package selection options: which packages get selected depends on the selected manifest file (which is based on the current working directory, unless you passed --manifest-path). If the manifest is the root of a workspace, then the workspace's default members get selected. Otherwise, only the package defined by the 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, then:

  • a virtual workspace will include all workspace members β€” which is equivalent to passing --workspace
  • a non-virtual workspace will include only the root crate itself
  • -p spec…, --package spec… β€” document only the packages you name. For the SPEC format, go peep cargo-pkgid(1). You can pass this flag multiple times, and it supports common Unix glob patterns like *, ? and []. BUT β€” and this is the important bit β€” to stop your shell from accidentally expanding those glob patterns before Cargo even gets its hands on them, you must wrap each pattern in single quotes or double quotes. fr fr. πŸ™
  • --workspace β€” document every single member in the workspace. the whole friend group πŸ’ž
  • --all β€” deprecated alias for --workspace. it's giving legacy, it's giving "we don't do that anymore" πŸ’€
  • --exclude *SPEC*… β€” exclude the packages you name. This one must be used together with the --workspace flag, no solo runs allowed. It can be specified multiple times and it supports common Unix glob patterns like *, ? and []. Same deal as before: to avoid your shell expanding those globs before Cargo handles them, you must use single quotes or double quotes around each pattern. πŸŽ€

🎯 Target Selection 🎯

When you give no target selection options at all, 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 are also skipped if they have required-features that are missing.

The default behaviour can be changed by setting doc = false for the target in the manifest settings. And here's the tea: 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 binary you name. Can be specified multiple times, and supports common Unix glob patterns.
  • --bins β€” document all binary targets 🧚
  • --example *name*… β€” document the example you name. Can be specified 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 you give no feature options, the default feature is activated for every selected package.

For the full details, see the features documentation over at https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options

  • -F features, --features features β€” a space-separated or comma-separated list of features to activate. Features of workspace members can be enabled with package-name/feature-name syntax. You can pass this flag multiple times, which enables all the features you specified. slay πŸ”₯
  • --all-features β€” activate every available feature of all selected packages. all of them. the entire buffet πŸ“
  • --no-default-features β€” do not 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 to get a list of supported targets. This flag may be specified multiple times.

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 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 want to 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.
  • --ignore-rust-version β€” document the target even if the selected Rust compiler is older than the required Rust version configured in the project's rust-version field. living dangerously, love that πŸ’…
  • --timings=fmts β€” output information about how long each compilation takes, and track concurrency information over time. It accepts an optional comma-separated list of output formats; --timings with no argument will default to --timings=html. Specifying an output format (rather than the default) is unstable and requires -Zunstable-options. Valid output formats:
    • html (unstable, requires -Zunstable-options): write 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, if you want to look at older runs. HTML output is suitable for human consumption only, and does not provide machine-readable timing data.
    • json (unstable, requires -Zunstable-options): emit machine-readable JSON information about timing information.

πŸ“ Output Options πŸ“

  • *--target-dir *directory** β€” the directory for all generated artifacts and intermediate files. May also be specified with the CARGO_TARGET_DIRenvironment variable, or thebuild.target-dirconfig value (https://doc.rust-lang.org/cargo/reference/config.html). Defaults totarget` in the root of the workspace. πŸ“‚

πŸ‘€ Display Options πŸ‘€

  • -v, --verbose β€” use verbose output. You can specify it 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). tell me everything bestie πŸ—£οΈ
  • -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 β€” control 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 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 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. πŸ”
  • --frozen, --locked β€” either one of these flags requires that the Cargo.lock file is up-to-date. If the lock file is missing, or if it needs to be updated, Cargo will exit with an error. The --frozen flag also prevents Cargo from attempting to access the network to determine whether it's out-of-date.

These can be used in environments where you want to assert that the Cargo.lock file is up-to-date (like a CI build), or where you want to avoid network access. 🚫🌐

  • --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 possible.
⚠️ Beware, bestie β€” 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).


🌟 Common Options 🌟

  • +toolchain β€” if Cargo has been installed with rustup, and the first argument to cargo begins with +, it will be interpreted as a rustup toolchain name (such as +stable or +nightly). See the rustup documentation (https://rust-lang.github.io/rustup/overrides.html) for more information about how toolchain overrides work. πŸ› οΈ
  • --config KEY=VALUE or PATH β€” overrides a Cargo configuration value, bestie πŸ’…. The argument has to be in TOML syntax of KEY=VALUE, or you hand it a path to an extra configuration file. This flag may be specified multiple times. Peep the command-line overrides section of the reference for more: <https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
  • -C PATH β€” changes the current working directory before executing any specified operations ✨. This affects things like where cargo looks by default for the project manifest (Cargo.toml), as well as the directories searched for discovering .cargo/config.toml, for example. This option must appear before the command name, for example:
cargo -C path/to/my-project build
ngl this one is lowkey exclusive β€” it's only available on the nightly channel <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and requires the -Z unstable-options flag to enable (see #10098 <https://github.com/rust-lang/cargo/issues/10098>).
  • -h, --help β€” prints help information. your lil "wait what do I do" button 🫢
  • -Z flag β€” unstable (nightly-only) flags to Cargo πŸ‘‘. Run cargo -Z help for details.

🌈 Miscellaneous Options 🌈

  • -j N, --jobs N β€” number of parallel jobs to run πŸƒβ€β™€οΈ. May also be specified with the build.jobs config value <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to the number of logical CPUs. If negative, it sets the maximum number of parallel jobs to the number of logical CPUs plus the value you provided. If a string default is provided, it sets the value back to defaults. Should not be 0. 🚫
  • --keep-going β€” build as many crates in the dependency graph as possible, rather than aborting the build on the first one that fails to build. she does NOT give up πŸ’ͺ

For example if the current package depends on dependencies fails and works, one of which fails to build, cargo doc -j1 may or may not build the one that succeeds (depending on which one of the two builds Cargo picked to run first), whereas cargo doc -j1 --keep-going would definitely run both builds, even if the one run first fails. fr fr, that's the whole point πŸŽ€


🌸 ENVIRONMENT 🌸

See the reference <https://doc.rust-lang.org/cargo/reference/environment-variables.html> for details on environment variables that Cargo reads πŸ‘€.


πŸ’€ EXIT STATUS πŸ’€

  1. 0: Cargo succeeded. slay ✨
  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)


okay that's the whole thing bestie, you're officially certified πŸŽ€ the footer on this one just says CARGO-DOC(1) β€” that's the Cargo manual's page stamp (source, version and date all rolled into that one lil line at the bottom of your local copy). go forth and document, icon πŸ’œ

πŸ“¦ where this page comes from