CARGO-DOC(1)
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 theBROWSERenvironment variable, or unless you've set thedoc.browserconfig 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
-pspecβ¦,--packagespecβ¦ β document only the packages you name. For the SPEC format, go peepcargo-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--workspaceflag, 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
-Ffeatures,--featuresfeatures β a space-separated or comma-separated list of features to activate. Features of workspace members can be enabled withpackage-name/feature-namesyntax. 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 βοΈ
--targettriple β document for the given architecture. The default is the host architecture. The general format of the triple is<arch><sub>-<vendor>-<sys>-<abi>. Runrustc --print target-listto 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 thereleaseprofile. See also the--profileoption if you want to pick a specific profile by name.--profilename β 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'srust-versionfield. 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;--timingswith 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 filecargo-timing.htmlto thetarget/cargo-timingsdirectory 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 theCARGO_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 theterm.verboseconfig 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 theterm.quietconfig value (https://doc.rust-lang.org/cargo/reference/config.html). shhh π€«--colorwhen β 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-formatfmt β 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 withshortandjson.short: emit shorter, human-readable text messages. Conflicts withhumanandjson.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 withhumanandshort.json-diagnostic-short: ensure therenderedfield of JSON messages contains the "short" rendering from rustc. Cannot be used withhumanorshort.json-diagnostic-rendered-ansi: ensure therenderedfield of JSON messages contains embedded ANSI color codes for respecting rustc's default color scheme. Cannot be used withhumanorshort.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 withhumanorshort.
π Manifest Options π
--manifest-pathpath β path to theCargo.tomlfile. By default, Cargo searches for theCargo.tomlfile in the current directory or any parent directory. π--frozen,--lockedβ either one of these flags requires that theCargo.lockfile is up-to-date. If the lock file is missing, or if it needs to be updated, Cargo will exit with an error. The--frozenflag 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 tocargobegins with+, it will be interpreted as a rustup toolchain name (such as+stableor+nightly). See the rustup documentation (https://rust-lang.github.io/rustup/overrides.html) for more information about how toolchain overrides work. π οΈ--config KEY=VALUEorPATHβ overrides a Cargo configuration value, bestie π . The argument has to be in TOML syntax ofKEY=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 π. Runcargo -Z helpfor details.
π Miscellaneous Options π
-j N,--jobs Nβ number of parallel jobs to run πββοΈ. May also be specified with thebuild.jobsconfig 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 stringdefaultis provided, it sets the value back to defaults. Should not be0. π«--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 π
0: Cargo succeeded. slay β¨101: Cargo failed to complete. π
π¦ EXAMPLES π¦
- 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
- ubuntu jammy: cargo-1.76 1.76.0+dfsg0ubuntu1~bpo0-0ubuntu0.22.04.1 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.76 1.76.0+dfsg0ubuntu1-0ubuntu0.24.04.2 Β· MIT OR Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms