CARGO-BUILD(1)
π CARGO-BUILD π
okay bestie, welcome to cargo-build β straight out of the General Commands Manual, section 1, and it's literally the command that turns your little pile of Rust source into actual compiled artifacts. this is part 1 of the page so buckle up, there's more coming π«Ά
β¨ NAME β¨
cargo-build β compile the current package. that's it. that's the whole job. π
πΈ SYNOPSIS πΈ
cargo build [options]
π¦ DESCRIPTION π¦
it compiles your local packages and all of their dependencies. yes, all of them. the whole tree. it's giving "responsible adult who does the homework for everyone" energy π
π OPTIONS π
π Package Selection
so here's the vibe: by default, when you don't pass any package selection options at all, the packages that get selected depend on the manifest file you've got selected (which is based on your current working directory, unless you gave it --manifest-path). if that manifest is the root of a workspace, then the workspace's default members get selected. otherwise, only the package defined by that manifest is selected. slay, that's the default behaviour. π
you can also set the default members of a workspace explicitly with the workspace.default-members key in the root manifest. if you don't set that, then:
- a virtual workspace will include all workspace members (which is exactly the same as passing
--workspace), and - a non-virtual workspace will include only the root crate itself.
-p specβ¦,--package specβ¦β build only the packages you name. the SPEC format lives over incargo-pkgid(1)if you need the deets. you can pass this flag multiple times, and it supports common Unix glob patterns like*,?and[]. BUT β and this is the part everyone gets wrong π β to stop your shell from expanding those globs before Cargo even gets a look at them, you have to wrap each pattern in single quotes or double quotes. do not skip this. the shell is feral.--workspaceβ build every single member in the workspace. all of them. no crumbs left. π₯--allβ this is a deprecated alias for--workspace. it still works but bestie it's giving legacy, just use--workspaceπ--exclude SPECβ¦β exclude the packages you name. must be used together with the--workspaceflag, like they're holding hands π€. can be specified multiple times, supports common Unix glob patterns like*,?and[], and yep β same rule as above, wrap each pattern in single or double quotes so your shell doesn't go rogue and expand it first.
π― Target Selection
when you don't give any 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. (bye bye, you weren't invited π
)
here's a fun one: binary targets are automatically built if there's an integration test or benchmark being selected to build. why? so that an integration test can execute the binary and exercise and test its behaviour. 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. iconic. β
and if you do pass target selection flags, it'll build only the targets you specified.
heads up bestie:--bin,--example,--testand--benchflags also support common Unix glob patterns like*,?and[]. and again β to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must use single quotes or double quotes around each glob pattern. i'm not repeating this a third time... okay maybe i will.
--libβ build the package's library.--bin nameβ¦β build the binary you named. can be specified multiple times, supports common Unix glob patterns.--binsβ build all binary targets.--example nameβ¦β build the example you named. can be specified multiple times, supports common Unix glob patterns.--examplesβ build all example targets.--test nameβ¦β build the integration test you named. can be specified multiple times, supports common Unix glob patterns.--testsβ build all targets in test mode that have thetest = truemanifest flag set. by default this includes the library and binaries built as unittests, plus integration tests. be aware π this will also build any required dependencies, so the lib target may get built twice (once as a unittest, and once as a dependency for binaries, integration tests, etc.). targets can be enabled or disabled by setting thetestflag in the manifest settings for the target.--bench nameβ¦β build the benchmark you named. can be specified multiple times, supports common Unix glob patterns.--benchesβ build all targets in benchmark mode that have thebench = truemanifest flag set. by default this includes the library and binaries built as benchmarks, plus bench targets. be aware π this will also build any required dependencies, so the lib target may get built twice (once as a benchmark, and once as a dependency for binaries, benchmarks, etc.). targets can be enabled or disabled by setting thebenchflag in the manifest settings for the target.--all-targetsβ build all targets. this is equivalent to specifying--lib --bins --tests --benches --examples. one flag, everything, no cap. π
π Feature Selection
the feature flags let you control which features get enabled. when you don't give any feature options, the default feature is activated for every selected package. the features documentation (https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options) has more details if you wanna go deeper.
-F features,--features featuresβ a space or comma separated list of features to activate. features of workspace members can be enabled with thepackage-name/feature-namesyntax. can be specified multiple times, and doing so enables all the specified features. all of them. obsessed. π«Ά--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. the opposite vibe. π
βοΈ Compilation Options
--target tripleβ 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 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 docs (https://doc.rust-lang.org/cargo/guide/build-cache.html) for more details. don't be shocked when your stuff shows up somewhere else. π
-r,--releaseβ build optimized artifacts with thereleaseprofile. also peep the--profileoption if you wanna choose a specific profile by name. this is the "it's going fast now" button π₯--profile nameβ build with the given profile. the reference (https://doc.rust-lang.org/cargo/reference/profiles.html) has more details on profiles.--timings=fmtsβ output info about how long each compilation takes, and track concurrency information over time. accepts an optional comma-separated list of output formats;--timingswith no argument defaults 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. it also writes a report to the same directory with a timestamp in the filename, if you wanna look at older runs. HTML output is suitable for human consumption only, and does not provide machine-readable timing data. (it's for your eyeballs, not your scripts. π)json(unstable, requires-Zunstable-options): emit machine-readable JSON information about timing information. for the robots π€
π¦ Output Options
--target-dir directoryβ the 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. yes, thattargetfolder. the chonky one. π--artifact-dir directoryβ 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. so if you're on stable, this one is not for you bestie. π§
π₯οΈ 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 double-vjust unlocks a whole extra layer of information π-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). silence. peace. πΈ--color whenβ 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-format fmtβ 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 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 have Cargo itself render the JSON diagnostics coming fromrustc. Cargo's own JSON diagnostics and others coming fromrustcare still emitted. cannot be used withhumanorshort.
--build-planβ outputs a series of JSON messages tostdoutthat 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. nightly girlies only. β¨
π Manifest Options π
--manifest-pathpath β where urCargo.tomllives, bestie. By default Cargo goes hunting forCargo.tomlin the current directory or any parent directory, but this lets u just point at it directly.--ignore-rust-versionβ ignore therust-versionspecification in packages. like. pretend it's not there. iconic of u honestly.--lockedβ this one asserts that the exact same dependencies and versions get used as when the existingCargo.lockfile was originally generated. Cargo will exit with an error when either of these scenarios pops up:- The lock file is missing. π
- Cargo attempted to change the lock file due to a different dependency resolution.
Use it in environments where u want deterministic builds β CI pipelines, for example. reproducibility queen behavior.
--offlineβ prevents Cargo from accessing the network for any reason. Without this flag, Cargo stops with an error if it needs the network and the network isn't available. With this flag, Cargo will attempt to proceed without the network if possible.
β οΈ Beware, bestie β this may result in different dependency resolution than online mode. Cargo restricts itself to crates that are downloaded locally, even if there might be a newer version as indicated in the local copy of the index. Wanna download dependencies before going offline? See the cargo-fetch(1) command. π«Ά
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 flags in a trench coat, slay.--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 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 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-optionsflag to enable (see #14421 βhttps://github.com/rust-lang/cargo/issues/14421).
β¨ Common Options β¨
+toolchain β if Cargo has been installed with rustup, and the first argument tocargobegins with+, it will be interpreted as a rustup toolchain name (such as+stableor+nightly). See the rustup documentation (https://rust-lang.github.io/rustup/overrides.html) for more info about how toolchain overrides work. toolchain switch-up, no cap--configKEY=VALUE or PATH β overrides a Cargo configuration value. The argument should be in TOML syntax ofKEY=VALUE, or provided as a path to an extra configuration file. 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 information.-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, for examplecargo -C path/to/my-project build. β¨ order matters bestie β¨
π§ 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-optionsflag to enable (see #10098 βhttps://github.com/rust-lang/cargo/issues/10098).
-h,--helpβ prints help information. ur lil emergency exit when u forget everything. π-Zflag β unstable (nightly-only) flags to Cargo. Runcargo -Z helpfor details. nightly girlies only
π 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 be 0. π«--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. perseverance era
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.
--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. secret little configs everywhere, iykyk
π EXIT STATUS π
0: Cargo succeeded. π we ate101: Cargo failed to complete. π it's giving tragedy
β EXAMPLES β
- Build the local package and all of its dependencies:
cargo build
- Build with optimizations:
cargo build --release
π SEE ALSO π
cargo(1), cargo-rustc(1) π¦
and that's the whole page, bestie!! β¨ this was the CARGO-BUILD(1) manual, coming to u from the Cargo source/version that ships it, dated with the timestamp on that footer line β u know the one, that lonely lil CARGO-BUILD(1) at the bottom doing main character energy all by itself. go build something cute. periodt. π
π
π¦ 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