CARGO-DOC(1)
π 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 theBROWSERenvironment variable, or use thedoc.browserconfiguration 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 outcargo-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--workspaceflag. 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.
-Ffeatures,--featuresfeatures β Space or comma separated list of features to activate. Features of workspace members can be enabled with thepackage-name/feature-namesyntax. 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 βοΈ
--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-listfor 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 thereleaseprofile. See also the--profileoption if you wanna 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.--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;--timingswith 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 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, 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-dirdirectory β 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 totargetin 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 theterm.verboseconfig value (https://doc.rust-lang.org/cargo/reference/config.html).-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 β 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-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, so it respects rustc's default color scheme. Cannot be used withhumanorshort.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 withhumanorshort.
π Manifest Options π
--manifest-pathpath β Path to theCargo.tomlfile. By default, Cargo searches for theCargo.tomlfile in the current directory or any parent directory.--ignore-rust-versionβ Ignore therust-versionspecification in packages. pretend you didn't see it. π--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 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--lockedand--offline. two for one, slay.--lockfile-pathPATH β Changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) toPATH.PATHmust 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 fromPATH, or write a new lockfile into the providedPATHif it doesn't exist. This flag can be used to run most commands in read-only directories, writing the lockfile into the providedPATH. β¨
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 aftercargostarts with+, it gets read as a rustup toolchain name (think+stableor+nightly). Wanna know how toolchain overrides actually work? The rustup documentation <https://rust-lang.github.io/rustup/overrides.html> has you covered. π¦--config KEY=VALUEorPATHβ overrides a Cargo config value, no big deal. The argument is either TOML syntax inKEY=VALUEform, 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, likecargo -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. Runcargo -Z helpfor the details.
π Miscellaneous Options π
-j N,--jobs Nβ how many parallel jobs to run. You can also set this with thebuild.jobsconfig 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 stringdefault, it sets the value back to defaults. And it should not be0. 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 β¨
0: Cargo succeeded. π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)
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
- ubuntu jammy: cargo-1.82 1.82.0+dfsg0ubuntu0~jammy-0ubuntu0.22.04.1 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.82 1.82.0+dfsg0ubuntu0-0ubuntu0.24.04.1 Β· 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