CARGO-RUSTDOC(1)
CARGO-RUSTDOC π
cargo-rustdoc β like, build your package's docs but with your own custom flags, bestie. It's a section 1 (General Commands Manual) moment, so grab a snack π
β¨ NAME β¨
cargo-rustdoc β Build a packageβs documentation, using specified custom flags
πΈ SYNOPSIS πΈ
cargo rustdoc [options] [-- args]
π¦ DESCRIPTION π¦
okay so here's the tea β β the specified target for the current package (or the package you named with -p if you did that) gets documented, and whatever args you pass get handed straight to the final rustdoc invocation. Dependencies do NOT get documented as part of this command, periodt. And just so you know: rustdoc will still unconditionally receive arguments like -L, --extern, and --crate-type no matter what β your args just get added on top of those. π
For the full rundown on rustdoc flags, go look at <https://doc.rust-lang.org/rustdoc/index.html>. π
This command requires that only one target is being compiled when you provide additional arguments. So if your current package has more than one target available, you've gotta use the filters --lib, --bin, etc, to pick which target gets compiled. no skipping this step fr fr π
Wanna pass flags to all the rustdoc processes that Cargo spawns? Use the RUSTDOCFLAGS environment variable <https://doc.rust-lang.org/cargo/reference/environment-variables.html> or the build.rustdocflags config value <https://doc.rust-lang.org/cargo/reference/config.html>. π«Ά
π OPTIONS π
π Documentation Options π
--openβ Opens the docs in a browser once they're built. It'll use your default browser unless you defined another one in theBROWSERenvironment variable, or you use thedoc.browserconfiguration option<https://doc.rust-lang.org/cargo/reference/config.html#docbrowser>. π
π¦ Package Selection π¦
By default, the package in the current working directory is the one selected. The -p flag lets you choose a different package in a workspace. π
-pspec,--packagespec β The package to document. Peepcargo-pkgid(1)for the SPEC format. π
π― Target Selection π―
When you give no target selection options at all, cargo rustdoc 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. π
Passing target selection flags will document only the targets you specified. β¨
Note that--bin,--example,--testand--benchflags also support common Unix glob patterns like*,?and[]. BUT β to avoid your shell accidentally expanding glob patterns before Cargo gets to handle them, you must use single quotes or double quotes around each glob pattern. π
--libβ Document the packageβs library. π--binnameβ¦ β Document the specified binary. This flag may be specified multiple times and supports common Unix glob patterns.--binsβ Document all binary targets. π₯--examplenameβ¦ β Document the specified example. This flag may be specified multiple times and supports common Unix glob patterns.--examplesβ Document all example targets. β¨--testnameβ¦ β Document the specified integration test. This flag may be specified multiple times and supports common Unix glob patterns.--testsβ Document all targets that have thetest = truemanifest flag set. By default this includes the library and binaries built as unittests, and integration tests. Be aware that this will also build any required dependencies, so the lib target may be built twice (once as a unittest, and once as a dependency for binaries, integration tests, etc.). Targets may be enabled or disabled by setting thetestflag in the manifest settings for the target. π--benchnameβ¦ β Document the specified benchmark. This flag may be specified multiple times and supports common Unix glob patterns.--benchesβ Document all targets that have thebench = truemanifest flag set. By default this includes the library and binaries built as benchmarks, and bench targets. Be aware that this will also build any required dependencies, so the lib target may be built twice (once as a benchmark, and once as a dependency for binaries, benchmarks, etc.). Targets may be enabled or disabled by setting thebenchflag in the manifest settings for the target. π--all-targetsβ Document all targets. This is equivalent to specifying--lib --bins --tests --benches --examples. the whole menu, no cap π½οΈ
π 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.
-Ffeatures,--featuresfeatures β Space or comma separated list of features to activate. Features of workspace members may be enabled withpackage-name/feature-namesyntax. This flag may be specified multiple times, which enables all specified features. π--all-featuresβ Activate all available features of all selected packages. everything, all at once, iconic β¨--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-listfor 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/reference/build-cache.html> for more details. ποΈ
-r,--releaseβ Document optimized artifacts with thereleaseprofile. See also the--profileoption for choosing 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β Output information how long each compilation takes, and track 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): Write a human-readable filecargo-timing.htmlto thetarget/cargo-timingsdirectory with a report of the compilation. Also write 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-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. May be specified 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>. π£οΈ-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>. π€«--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 fromrustc. Cannot be used withhumanorshort.json-diagnostic-rendered-ansi: Ensure therenderedfield of JSON messages contains embedded ANSI color codes for respectingrustcβs default color scheme. Cannot be used withhumanorshort.json-render-diagnostics: Instruct Cargo to not includerustcdiagnostics in JSON messages printed, but instead Cargo itself should render the JSON diagnostics coming fromrustc. Cargoβs own JSON diagnostics and others coming fromrustcare 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β Ignorerust-versionspecification in packages. π--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. π
--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. βοΈ
ok so continuing from the --offline of it all β because bestie, going offline is a whole mood and it comes with lore:
β οΈ be careful, because offline mode can give you a different dependency resolution than online mode. Cargo will restrict itself to crates that are downloaded locally, even if there's a newer version just sitting there indicated in your local copy of the index. π so if you want your deps already on deck before you yeet yourself offline, go look at the cargo-fetch(1) command first.
this can also be set with the net.offline config value (https://doc.rust-lang.org/cargo/reference/config.html).
--frozenβ literally just the same as specifying both--lockedand--offlineat the same time. two-for-one, slay. π--lockfile-pathPATH β moves the lockfile's path away from the default (<workspace_root>/Cargo.lock) and over to PATH. PATH must end withCargo.lock(like, for example,--lockfile-path /tmp/temporary-lockfile/Cargo.lock). heads up though: giving--lockfile-pathmakes Cargo ignore any existing lockfile at the default path, and instead it'll either use the lockfile from PATH, or write a brand new lockfile into the PATH you gave it if nothing's there yet. the whole point? it lets you run most commands in read-only directories by writing the lockfile into the PATH you provided. no cap, iconic behavior. β¨
π¨ 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 (see #14421 β https://github.com/rust-lang/cargo/issues/14421).
π«Ά Common Options π«Ά
+toolchainβ if Cargo got installed with rustup, and the first argument tocargostarts with+, then Cargo reads it as a rustup toolchain name (think+stableor+nightly). want the whole story on how toolchain overrides work? it's in the rustup documentation: https://rust-lang.github.io/rustup/overrides.html--configKEY=VALUE or PATH β overrides a Cargo configuration value, bestie. the argument needs to be in TOML syntax asKEY=VALUE, or you can hand it a path to an extra configuration file. you can pass this flag multiple times, so go off. more deets in the command-line overrides section: https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides-CPATH β switches up the current working directory before it executes any of the operations you asked for. this affects stuff like where cargo looks by default for the project manifest (Cargo.toml), and also the directories it searches to discover.cargo/config.toml, for example. this option has to appear before the command name, like this:
cargo -C path/to/my-project build
π¨ again β nightly channel only (https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and it requires the -Z unstable-options flag to enable (see #10098 β https://github.com/rust-lang/cargo/issues/10098).
-h,--helpβ prints help information. the classic. the icon. π-Zflag β the unstable, nightly-only flags for Cargo. runcargo -Z helpfor the tea. β
π Miscellaneous Options π
-jN,--jobsN β how many parallel jobs to run. can also be set 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 the value you gave. and if you pass the stringdefault, it sets the value back to defaults. π oh and β it should not be 0.--keep-goingβ builds as many crates in the dependency graph as it possibly can, instead of aborting the whole build the second one of them fails to build. it's giving "we don't stop, we keep going" energy.
for example, if the current package depends on dependencies fails and works, and one of them fails to build, then cargo rustdoc -j1 may or may not build the one that succeeds (it depends on which one of the two builds Cargo picked to run first π), whereas cargo rustdoc -j1 --keep-going would definitely run both builds, even if the one it ran first fails. consistent queen. π
--output-formatβ the output type for the documentation emitted. valid values are:html(default): emit the documentation in HTML format.json: emit the documentation in the experimental JSON format (https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc_json_types).
π¨ yep, you guessed it β this one 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.
πΈ ENVIRONMENT πΈ
want the full list of environment variables that Cargo reads? it's all in the reference, bestie: https://doc.rust-lang.org/cargo/reference/environment-variables.html
β EXIT STATUS β
0: Cargo succeeded. π we love a happy ending.101: Cargo failed to complete. π it's giving tragedy.
π EXAMPLES π
- Build documentation with custom CSS included from a given file:
cargo rustdoc --lib -- --extend-css extra.css
π SEE ALSO π
cargo(1), cargo-doc(1), rustdoc(1)
and that's the whole page, periodt. π this was CARGO-RUSTDOC(1) β the source/version and the date it was generated are chilling down in the footer of your local copy, so peek there for the receipts. now go document something and be the main character of your own build. β¨π¦π
π¦ where this page comes from
- ubuntu jammy: cargo-1.85 1.85.1+dfsg0ubuntu2~bpo0-0ubuntu1.22.04.1 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.85 1.85.1+dfsg0ubuntu2~bpo0-0ubuntu0.24.04.2 Β· Apache-2.0 WITH LLVM exception
- ubuntu questing: cargo-1.85 1.85.1+dfsg0ubuntu2-0ubuntu1
- ubuntu questing: cargo-1.85 1.85.1+dfsg0ubuntu2-0ubuntu1.25.04.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms