CARGO-BUILD(1)
π CARGO-BUILD(1) β like, the whole entire manual, bestie
okay so this is the cargo-build page from the General Commands Manual section, and it is a whole journey. buckle up. π
β¨ NAME β¨
cargo-build β compile the current package. that's it. that's the girl. π
β¨ SYNOPSIS β¨
cargo build [options]
β¨ DESCRIPTION β¨
Compiles your local packages and literally every single one of their dependencies. the whole tree. no cap. π³
β¨ OPTIONS β¨
π Package Selection
okay so by default, when you don't hand it any package selection options, which packages get picked depends on the selected manifest file (and that's based on the current working directory if you didn't pass --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. that's the vibe. π
You can set the default members of a workspace explicitly with the workspace.default-members key in the root manifest. If that's not set, then:
- a virtual workspace includes all workspace members (equivalent to passing
--workspace) - a non-virtual workspace includes only the root crate itself
-p*spec*β¦,--package*spec*β¦ β build only the packages you named. the SPEC format lives over incargo-pkgid(1). you can pass this flag multiple times, and it supports common Unix glob patterns like*,?and[]. BUT β and this part matters bestie β so your shell doesn't accidentally expand those glob patterns before Cargo even gets to handle them, you must use single quotes or double quotes around each pattern. π--workspaceβ build every member in the workspace. all of them. periodt.--allβ deprecated alias for--workspace. it's giving legacy. still works tho.--exclude*SPEC*β¦ β exclude the packages you named. must be used in conjunction with the--workspaceflag, fr fr. can be specified multiple times and supports common Unix glob patterns like*,?and[]. and same warning as before: to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must use single quotes or double quotes around each pattern. π
π Target Selection
When you give it no target selection options, cargo build will build all binary and library targets of the selected packages. Binaries get skipped if they have required-features that are missing. π
Binary targets are automatically built if there's an integration test or benchmark being selected to build. this is so an integration test can actually execute the binary to exercise and test its behavior. The CARGO_BIN_EXE_<name> environment variable (https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates) gets set when the integration test is built, so it can use the env macro (https://doc.rust-lang.org/std/macro.env.html) to locate the executable. cute, right? π¦
Passing target selection flags will build only the specified targets.
heads up:--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. no exceptions bestie. β¨
--libβ build the package's library.--bin*name*β¦ β build the specified binary. may be specified multiple times and supports common Unix glob patterns.--binsβ build all binary targets.--example*name*β¦ β build the specified example. may be specified multiple times and supports common Unix glob patterns.--examplesβ build all example targets.--test*name*β¦ β build the specified integration test. may be specified multiple times and supports common Unix glob patterns.--testsβ build all targets that have thetest = truemanifest flag set. by default this includes the library and binaries built as unittests, plus 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.--bench*name*β¦ β build the specified benchmark. may be specified multiple times and supports common Unix glob patterns.--benchesβ build all targets that have thebench = truemanifest flag set. by default this includes the library and binaries built as benchmarks, plus 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β build all targets. equivalent to specifying--lib --bins --tests --benches --examples. the full set, the whole menu. π₯
π 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.--no-default-featuresβ do not activate the default feature of the selected packages.
π Compilation Options
--targettriple β build 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β build optimized artifacts with thereleaseprofile. see also the--profileoption for choosing a specific profile by name.--profilename β build 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.--artifact-dirdirectory β copy final artifacts to this directory.
this option is unstable and available only on the nightly channel (https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and requires the-Z unstable-optionsflag to enable. seehttps://github.com/rust-lang/cargo/issues/6790for more information. π
π 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 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 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.
--build-planβ outputs a series of JSON messages to stdout that indicate the commands to run the build.
this option is unstable and available only on the nightly channel (https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and requires the-Z unstable-optionsflag to enable. seehttps://github.com/rust-lang/cargo/issues/5579for more information. π
β¨ Manifest Options β¨
--manifest-pathpath β tells Cargo exactly where yourCargo.tomllives. By default Cargo just goes hunting for aCargo.tomlin the current directory, and if it's not there, it keeps climbing up through every parent directory until it finds one.--ignore-rust-versionβ straight up ignores therust-versionspec that packages declare. Like, "I know you said you need this Rust, bestie, but we're doing it anyway."--lockedβ swears up and down that the exact same dependencies and versions get used as when the existingCargo.lockfile was first generated. Cargo will bail with an error the moment either of these happens:- The lock file is missing. π
- Cargo tried to change the lock file because of a different dependency resolution.
It's giving very "certified deterministic build" energy, so it's perfect for environments where you need that, like CI pipelines.
--offlineβ blocks Cargo from touching the network for literally any reason. Without this flag, Cargo stops dead with an error if it needs the network and the network isn't there. With this flag, Cargo tries to keep going without the network if it can.
Heads up bestie: this can totally 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 sitting right there as indicated in the local copy of the index. Peep cargo-fetch(1) to download dependencies before you go offline.
May also be specified with the net.offline config value <https://doc.rust-lang.org/cargo/reference/config.html>.
--frozenβ the combo meal. Equivalent to specifying both--lockedand--offlineat the same time. π§--lockfile-pathPATH β changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) over to PATH. PATH must end withCargo.lock(e.g.--lockfile-path /tmp/temporary-lockfile/Cargo.lock). Real talk: providing--lockfile-pathwill ignore the existing lockfile at the default path, and instead will either use the lockfile from PATH, or write a brand 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.
This option is 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 #14421 <https://github.com/rust-lang/cargo/issues/14421>).
πΈ Common Options πΈ
+toolchain β if Cargo was installed with rustup, and the first argument tocargostarts with+, Cargo reads it as a rustup toolchain name (think+stableor+nightly). See the rustup documentation <https://rust-lang.github.io/rustup/overrides.html> for more on how toolchain overrides work.--configKEY=VALUE or PATH β overrides a Cargo configuration value. 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. See the command-line overrides section <https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides> for more.-CPATH β 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, likecargo -C path/to/my-project build.
This option is 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. the way that this is always there for you? π«Ά-Zflag β unstable (nightly-only) flags to Cargo. Runcargo -Z helpfor the deets.
π Miscellaneous Options π
-jN,--jobsN β 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 provided value. If a stringdefaultis provided, it sets the value back to defaults. Should not be0.--keep-goingβ builds as many crates in the dependency graph as possible, rather than aborting the whole build on the first one that fails.
For example, if the current package depends on dependencies fails and works, one of which fails to build, cargo build -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 build -j1 --keep-going would definitely run both builds, even if the one run first fails. no cap, it keeps going. π₯
--future-incompat-reportβ displays a future-incompat report for any future-incompatible warnings produced during execution of this command.
See cargo-report(1).
π 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 and all of its dependencies:
cargo build
- Build with optimizations:
cargo build --release
π¦ SEE ALSO π¦
okay bestie that's the whole page!! π this was CARGO-BUILD(1) from the Cargo manual β your girl Cargo, published by the Rust Project, and dated whenever this version of the docs got cut. go build something iconic, you've got this ππΈ
π¦ where this page comes from
- ubuntu jammy: cargo-1.84 1.84.1+dfsg0ubuntu1~bpo10-0ubuntu4.22.04.1 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.84 1.84.1+dfsg0ubuntu1~bpo2-0ubuntu2.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