CARGO-DOC(1)
β¨ NAME β¨
cargo-doc β it builds a package's documentation, no cap. πβ¨
Manual name: CARGO-DOC(1), General Commands Manual. π
π SYNOPSIS π
cargo doc [options]
π DESCRIPTION π
Bestie. It builds the documentation for your local package and all its dependencies. The output lands in target/doc, in rustdoc's usual format. That's the whole vibe. π¦
πΈ OPTIONS πΈ
π Documentation Options π
--openβ Opens the docs in a browser once they're built. It'll use your default browser unless you define another one in theBROWSERenvironment variable, or use thedoc.browserconfiguration option (https://doc.rust-lang.org/cargo/reference/config.html#docbrowser). Obsessed with convenience, honestly.--no-depsβ Do not build documentation for dependencies. Just yours. Main character energy only. π--document-private-itemsβ Includes non-public items in the documentation. This gets enabled by default if you're documenting a binary target. (Yes, the private stuff. We're nosy now. π)
π§ Package Selection π§
So like, by default, when you don't pass any package selection options, the packages that get selected depend on the selected manifest file (based on the current working directory if --manifest-path isn't given). 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 isn't 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. Slay, that's the rule. π
-pspecβ¦,--packagespecβ¦ β Documents only the specified packages. Peepcargo-pkgid(1)for the SPEC format. This flag can be specified multiple times and supports common Unix glob patterns like*,?and[]. BUT β and this is the important part β to avoid your shell accidentally expanding glob patterns before Cargo gets its hands on them, you must use single quotes or double quotes around each pattern. Quote your patterns, bestie, I'm begging. π--workspaceβ Documents all members in the workspace. Everybody's invited to the party. π--allβ Deprecated alias for--workspace. It's giving legacy. Still works, but like, use the new one.--exclude*SPEC*β¦ β Excludes the specified packages. Must be used in conjunction with the--workspaceflag. This flag can 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. Same drill as before. π
π¦ 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. Iconic override behavior, fr fr. β
--libβ Documents the package's library.--bin*name*β¦ β Documents the specified binary. This flag can be specified multiple times and supports common Unix glob patterns.--binsβ Documents all binary targets.--example*name*β¦ β Documents the specified example. This flag can be specified multiple times and supports common Unix glob patterns.--examplesβ Documents all example targets.
π Feature Selection π
The feature flags let you control which features get 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.
-Ffeatures,--featuresfeatures β A space or comma separated list of features to activate. Features of workspace members can be enabled withpackage-name/feature-namesyntax. This flag can be specified multiple times, which enables all specified features. Stack 'em up. π₯--all-featuresβ Activates all available features of all selected packages. Everything. All of it. Periodt.--no-default-featuresβ Do not activate the default feature of the selected packages.
π Compilation Options π
--targettriple β Documents 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-listfor a list of supported targets. This flag can be specified multiple times.
This can also be specified with the build.target config value (https://doc.rust-lang.org/cargo/reference/config.html).
Heads up: 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/reference/build-cache.html) for more details.
-r,--releaseβ Documents optimized artifacts with thereleaseprofile. See also the--profileoption for choosing a specific profile by name. Glow-up mode for your docs. π--profilename β Documents with the given profile. See the reference (https://doc.rust-lang.org/cargo/reference/profiles.html) for more details on profiles.--timings=fmts β Outputs information about how long each compilation takes, and tracks concurrency information over time. Accepts an optional comma-separated list of output formats;--timingswithout an 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): Writes a human-readable filecargo-timing.htmlto thetarget/cargo-timingsdirectory with a report of the compilation. 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): Emits machine-readable JSON information about timing information.
π«Ά Output Options π«Ά
--target-dirdirectory β Directory for all generated artifacts and intermediate files. Can also be specified with theCARGO_TARGET_DIRenvironment variable, or thebuild.target-dirconfig value (https://doc.rust-lang.org/cargo/reference/config.html). Defaults totargetin the root of the workspace.
π Display Options π
-v,--verboseβ Use verbose output. Can be specified twice for "very verbose" output, which includes extra output such as dependency warnings and build script output. Can also be specified with theterm.verboseconfig value (https://doc.rust-lang.org/cargo/reference/config.html). Spill the tea, Cargo. β-q,--quietβ Do not print cargo log messages. Can also be specified with theterm.quietconfig value (https://doc.rust-lang.org/cargo/reference/config.html).--colorwhen β Controls when colored output is used. Valid values:auto(default): Automatically detects if color support is available on the terminal.always: Always displays colors.never: Never displays colors.
Can 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 tostdout. 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 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 withhumanorshort.
π Manifest Options π
--manifest-pathpath β Path to theCargo.tomlfile. By default, Cargo searches for theCargo.tomlfile in the current directory or any parent directory. It goes looking, like a little detective. π--ignore-rust-versionβ Ignoresrust-versionspecification in packages. Living dangerously, love that for you.--lockedβ Asserts that the exact same dependencies and versions are used as when the existingCargo.lockfile 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. This is the "we said what we said" flag. π
--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.
Can also be specified with the net.offline config value (https://doc.rust-lang.org/cargo/reference/config.html). Airplane mode but make it crates. βοΈ
--frozenβ Equivalent to specifying both--lockedand--offline. Two for the price of one, we love a bundle. π--lockfile-pathPATH β Changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) to PATH. PATH must end withCargo.lock(e.g.--lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that providing--lockfile-pathwill 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. Sneaky little redirect, but make it official. π
okay so this one's only a thing on the nightly channel https://doc.rust-lang.org/book/appendix-07-nightly-rust.html, and you need the -Z unstable-options flag turned on to even unlock it (peep issue #14421: https://github.com/rust-lang/cargo/issues/14421). stable girlies, this is not for you, sorry bestie π
πΈ Common Options πΈ
the little extras that basically every cargo subcommand shares, so you already know the vibe:
+toolchainβ if you installed Cargo through rustup and the very first thing you type aftercargostarts with a+, cargo goes "oh that's a rustup toolchain name" (think+stableor+nightly). Wanna know how toolchain overrides work? The rustup docs have you:https://rust-lang.github.io/rustup/overrides.htmlπ¦--config KEY=VALUEorPATHβ overrides a Cargo configuration value. You either hand it TOML in the formKEY=VALUE, or you give it a path to an extra config file to pull in. And yes, you can pass this flag more than once β stack 'em, queen π. 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 literally anything you asked for. So it messes with stuff like where cargo hunts for your project manifest (Cargo.toml) by default, and also the directories it searches when it's discovering.cargo/config.toml, for example. Heads up though: this option has to come before the command name, likecargo -C path/to/my-project build.
β οΈ warning bestie β οΈ 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 (issue #10098: https://github.com/rust-lang/cargo/issues/10098).
-h,--helpβ prints help information. the classic. the goat. when in doubt, ask for help π«Ά-Z flagβ unstable (nightly-only) flags for Cargo. runcargo -Z helpif you wanna see the details, iykyk β¨
π Miscellaneous Options π
the odds and ends that don't fit anywhere else but absolutely still matter:
-j N,--jobs Nβ how many parallel jobs to run at once. you can also set this with thebuild.jobsconfig value (https://doc.rust-lang.org/cargo/reference/config.html). by default it's however many logical CPUs you've got. if you give it a negative number, it sets the max parallel jobs to your number of logical CPUs plus the value you provided. and if you hand it a stringdefault, it just puts the value back to defaults. should not be0β fr fr, do not do that.--keep-goingβ builds as many crates in the dependency graph as it possibly can, instead of throwing its hands up and aborting the whole build the moment one of them fails. main character energy, honestly.
for example: say your current package depends on two dependencies, fails and works, and one of them fails to build. then cargo doc -j1 may or may not build the one that succeeds β it all depends on which of the two builds Cargo happened to pick to run first π. but cargo doc -j1 --keep-going would definitely run both builds, even if the one it ran first fails. no cap.
π ENVIRONMENT π
wanna know which environment variables Cargo reads? the reference has the full tea:
https://doc.rust-lang.org/cargo/reference/environment-variables.html
β EXIT STATUS β
so you know what the little number at the end means:
0: Cargo succeeded. π slay.101: Cargo failed to complete. π it's giving pain.
π¦ 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)
and that's the whole page bestie!! β¨ the footer signs off as CARGO-DOC(1), which is the page name plus the date stamp, so if you ever need to cite it just know it's the CARGO-DOC(1) manual page, periodt π
π«Ά go document something iconic π
π¦ where this page comes from
- ubuntu jammy: cargo-1.89 1.89.0+dfsg~24.04-0ubuntu0.22.04.2 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.89 1.89.0+dfsg~24.04-0ubuntu0.24.04.2 Β· Apache-2.0 WITH LLVM exception
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms