CARGO-RUSTDOC(1)
π CARGO-RUSTDOC β the cargo subcommand that builds your docs π
okay bestie, welcome to cargo-rustdoc, straight out of the General Commands Manual. this is the cargo command that builds a package's documentation, using specified custom flags β¨. she's the doc-building queen and she's about to spill everything, no gatekeeping π«Ά
β¨ SYNOPSIS β¨
cargo rustdoc [options] [-- args]
like, that's the whole vibe. options go before, and then -- args is where you slide in your custom rustdoc flags π
πΈ DESCRIPTION πΈ
so here's the tea, bestie. the target you pick for the current package β or the package you point at with -p if you gave one β gets documented, and the args you specify get passed straight into the final rustdoc invocation. it's giving "you have the aux cord" π§
Important though: dependencies will not be documented as part of this command. only the target you selected. periodt.
and like, don't worry β rustdoc still unconditionally receives arguments such as -L, --extern, and --crate-type no matter what. your specified args are just simply added on top of the rustdoc invocation π
want the full rundown on rustdoc flags? it's all here: <https://doc.rust-lang.org/rustdoc/index.html> π
also lowkey crucial: this command requires that only one target is being compiled when additional arguments are provided. if more than one target is available for the current package, you gotta use the filters--lib,--bin, etc. to select which target is compiled. no multitasking here, one target at a time π
and if you want to pass flags to all rustdoc processes spawned by Cargo (not just this one), 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>. that's the "apply to everyone" button fr fr π
π OPTIONS π
π Documentation Options
--openβ open the docs in a browser after building them. this uses your default browser unless you define another one in theBROWSERenvironment variable or use thedoc.browser<https://doc.rust-lang.org/cargo/reference/config.html#docbrowser> configuration option. it's giving "build it and immediately show it off" π¦
π Package Selection
by default, the package in the current working directory is selected. the -p flag can be used to choose a different package in a workspace. main character energy for whichever package you point at β¨
-pspec,--packagespec β the package to document. seecargo-pkgid(1)for theSPECformat. iykyk π
π― Target Selection
when no target selection options are given, cargo rustdoc 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 also skipped if they have required-features that are missing. so like, she curates for you automatically π
passing target selection flags will document only the specified targets.
β οΈ heads up bestie:--bin,--example,--testand--benchflags also support common Unix glob patterns like*,?and[]. BUT β to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must use single quotes or double quotes around each glob pattern. otherwise your shell will eat them and cargo never even sees them π no cap.
--libβ document the package's library. the classic π--bin*name*β¦ β document the specified binary. this flag may be specified multiple times and supports common Unix glob patterns. π₯--binsβ document all binary targets. all of them, collectively, slay π--example*name*β¦ β document the specified example. this flag may be specified multiple times and supports common Unix glob patterns. β¨--examplesβ document all example targets. every single one πΈ--test*name*β¦ β document the specified integration test. this flag may be specified multiple times and supports common Unix glob patterns. π§--testsβ document all targets in test mode 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. double the build, double the drama π--bench*name*β¦ β document the specified benchmark. this flag may be specified multiple times and supports common Unix glob patterns. β--benchesβ document all targets in benchmark mode 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. same double-build situation, we love a consistent queen π--all-targetsβ document all targets. this is equivalent to specifying--lib --bins --tests --benches --examples. the "everything, all at once" option π
π 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. defaults are cute like that π
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. stack 'em up π--all-featuresβ activate all available features of all selected packages. go big or go home π₯--no-default-featuresβ do not activate the default feature of the selected packages. we're going rogue, no defaults today π
π οΈ 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 <https://doc.rust-lang.org/cargo/guide/build-cache.html> documentation for more details. different directory, different vibe π¦
-r,--releaseβ document optimized artifacts with thereleaseprofile. see also the--profileoption for choosing a specific profile by name. optimized and iconic β¨--profilename β document with the given profile. see the reference <https://doc.rust-lang.org/cargo/reference/profiles.html> for more details on profiles. pick your fighter π₯--ignore-rust-versionβ document the target even if the selected Rust compiler is older than the required Rust version as configured in the project'srust-versionfield. ignoring the rules, living dangerously π--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. pretty colors for the humans only πjson(unstable, requires-Zunstable-options): emit machine-readable JSON information about timing information. for the robots π€
π¦ 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. home sweet home π
π£οΈ 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>. the way that twice = extra chatty π-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, silence π€«--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. rainbow mode on πnever: never display colors. monochrome era π€
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. the "cargo takes the wheel" option π
π Manifest Options
--manifest-pathpath β path to theCargo.tomlfile. by default, Cargo searches for theCargo.tomlfile in the current directory or any parent directory. it'll find it, she's thorough π--frozen,--lockedβ either of these flags requires that theCargo.lockfile is up-to-date. if the lock file is missing, or it needs to be updated, Cargo will exit with an error. the--frozenflag also prevents Cargo from attempting to access the network to determine if it is out-of-date.
these may be used in environments where you want to assert that the Cargo.lock file is up-to-date (such as a CI build) or want to avoid network access. CI besties, this one's for you π«Ά
--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. airplane mode but make it cargo βοΈ
to be continued, bestie β part 2 is coming π
Beware, bestie β this can give you different dependency resolution than online mode does. Cargo will restrict itself to crates that are already downloaded locally, even if there might be a newer version as indicated in the local copy of the index. So if you wanna go offline and still have everything you need, hit up the cargo-fetch(1) command first to download your dependencies. Prep work, periodt. π
You can also set this with the net.offline config value: https://doc.rust-lang.org/cargo/reference/config.html
π«Ά Common Options π«Ά
+toolchainβ okay so if Cargo got installed with rustup, and the first argument tocargostarts with+, it gets read as a rustup toolchain name (think+stableor+nightly, iconic). Peep the rustup documentation for the whole story on how toolchain overrides work:https://rust-lang.github.io/rustup/overrides.html--config KEY=VALUE or PATHβ overrides a Cargo config value, slay. The argument should be in TOML syntax ofKEY=VALUE, or you can hand it a path to an extra configuration file instead. This flag may be specified multiple times (stack 'em up, she's flexible β¨). More deets 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 executing any specified operations. This affects things like where cargo looks by default for the project manifest (Cargo.toml), plus the directories searched for discovering.cargo/config.toml, for example. Heads up: this option must appear before the command name, like this:
cargo -C path/to/my-project build
Lowkey important: this one's only available on the nightly channel (https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and needs the-Z unstable-optionsflag to turn on (see #10098,https://github.com/rust-lang/cargo/issues/10098). No stable crumbs for this one, sorry babe. π
-h,--helpβ prints help information. The classic. π«Ά-Z flagβ unstable (nightly-only) flags to Cargo. Runcargo -Z helpfor the full rundown, iykyk.
π Miscellaneous Options π
-j N,--jobs Nβ number of 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 maximum number of parallel jobs to the number of logical CPUs plus whatever value you provided. If a string default is provided, it sets the value back to defaults. And listen β it should not be0. Not even once. π--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. Main character energy: she does not stop for the drama. π
For example, if the current package depends on dependencies fails and works, one of which fails to build, cargo rustdoc -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 rustdoc -j1 --keep-going would definitely run both builds, even if the one run first fails. Consistency, we love to see it. β¨
πΈ ENVIRONMENT πΈ
See the reference for details on environment variables that Cargo reads: https://doc.rust-lang.org/cargo/reference/environment-variables.html
π EXIT STATUS π
0: Cargo succeeded. π (the good ending)101: Cargo failed to complete. π (the other ending)
β¨ 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, bestie β you officially know cargo-rustdoc(1) now, no cap. π This one lives in the Cargo manual alongside all her sisters, so go be nosy in cargo(1) too. Go forth and document your crates, queen β you ate. π
β¨π
π¦ 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