CARGO-BUILD(1)
β¨ CARGO-BUILD(1) β¨ β the cargo-build manual, but make it make sense π
okay bestie, this is the General Commands Manual entry for cargo-build, and it's giving "compile the current package" energy. Let's get into it π«Ά
πΈ NAME πΈ
cargo-build β Compile the current package
π SYNOPSIS π
cargo build [options]
π DESCRIPTION π
Compile local packages and all of their dependencies. that's it, that's the whole vibe. everything you wrote gets built, and so does every single thing it depends on. no cap.
π OPTIONS π
π§Έ Package Selection π§Έ
so like, by default β when you give zero package selection options β which packages get picked depends on the selected manifest file (which is based on the current working directory if you didn't pass --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 the manifest gets selected. periodt.
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 (which is equivalent to passing --workspace), and a non-virtual workspace will include only the root crate itself. iykyk.
-p*spec*β¦,--package*spec*β¦ β Build only the specified packages. Seecargo-pkgid(1)for the SPEC format. You can specify this flag multiple times and it supports common Unix glob patterns like*,?and[]. BUT β 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. no exceptions bestie.--workspaceβ Build all members in the workspace. all of them. the whole squad.--allβ Deprecated alias for--workspace. it still works, it's just old news.--exclude*SPEC*β¦ β Exclude the specified packages. Must be used in conjunction with the--workspaceflag (they're a package deal, literally). This flag may be specified multiple times and supports 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 pattern. same rules, different flag π
π¦ Target Selection π¦
When you give no target selection options, cargo build will build all binary and library targets of the selected packages. Binaries are skipped if they have required-features that are missing. sorry to those binaries, they're just not invited to the party π
Binary targets are automatically built if there's an integration test or benchmark being selected to build. Why? So the integration test can execute the binary to exercise and test its behavior. iconic, honestly. The CARGO_BIN_EXE_<name> environment variable <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates> is set when the integration test is built so that it can use the env macro <https://doc.rust-lang.org/std/macro.env.html> to locate the executable. very slay of it.
Passing target selection flags will build only the specified targets. so like, narrow it down.
β¨ heads up:--bin,--example,--testand--benchflags also support common Unix glob patterns like*,?and[]. However, to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must use single quotes or double quotes around each glob pattern. every single time. don't make me say it again π«Ά
--libβ Build the package's library.--bin*name*β¦ β Build the specified binary. This flag may be specified multiple times and supports common Unix glob patterns.--binsβ Build all binary targets.--example*name*β¦ β Build the specified example. This flag may be specified multiple times and supports common Unix glob patterns.--examplesβ Build all example targets.--test*name*β¦ β Build the specified integration test. This flag 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, 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. yeah, twice. it's a lot. it's fine. it's normal π--bench*name*β¦ β Build the specified benchmark. This flag 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, 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, same energy π--all-targetsβ Build all targets. This is equivalent to specifying--lib --bins --tests --benches --examples. the "give me everything" button.
π 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. that's the automatic behavior, bestie.
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. every single one. go off.--no-default-featuresβ Do not activate the default feature of the selected packages. the opposite button, we love her.
π§ 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 <https://doc.rust-lang.org/cargo/reference/build-cache.html> documentation for more details. so like, your stuff moves. don't panic.
-r,--releaseβ Build optimized artifacts with thereleaseprofile. See also the--profileoption for choosing a specific profile by name. this is the "make it fast" button, she takes a minute but she delivers β¨--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. pretty to look at, not for robots πjson(unstable, requires-Zunstable-options): Emit machine-readable JSON information about timing information. this one is 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. that's where all your build stuff lives, bestie.--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. See<https://github.com/rust-lang/cargo/issues/6790>for 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>. tell me everything, I want the tea π-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 β Control when colored output is used. Valid values:auto(default): Automatically detect if color support is available on the terminal. she's smart like that.always: Always display colors. rainbow mode on, no matter what π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 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 Cargo itself should 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. See<https://github.com/rust-lang/cargo/issues/5579>for more information.
okay that's the whole first half bestie, more is coming so don't go anywhere π
β¨ Manifest Options β¨
--manifest-pathpath β points Cargo straight at theCargo.tomlfile you want, no searching required. Without it, Cargo goes hunting forCargo.tomlin the current directory, and then keeps climbing up through every parent directory until it finds one. π--ignore-rust-versionβ tells Cargo to straight up ignore therust-versionspec that packages declare. Living dangerously, bestie. π--lockedβ this one is the assert flag: it demands that the exact same dependencies and exact same versions get used as when the existingCargo.lockfile was first generated. Cargo will bounce out with an error when either of these two little situations shows up:- The lock file is missing. π
- Cargo tried to change the lock file because the dependency resolution came out different.
Use it anywhere you want builds to be deterministic, like in CI pipelines. It's giving "no surprises allowed", and honestly? Iconic. π₯
--offlineβ blocks Cargo from touching the network for any reason. Without this flag, if Cargo needs the network and the network isn't there, it just stops with an error. With this flag, Cargo will try its best to keep going without the network if that's possible at all.
Careful though, because this can give you a different dependency resolution than online mode. Cargo restricts itself to crates that are already downloaded locally, even if the local copy of the index says a newer version exists. So if you're planning to go offline, peep the cargo-fetch(1) command to download your dependencies first.
You can also set this with the net.offline config value <https://doc.rust-lang.org/cargo/reference/config.html>.
--frozenβ the combo meal: it's exactly equivalent to passing both--lockedand--offline. π--lockfile-pathPATH β moves the lockfile away from its default home (<workspace_root>/Cargo.lock) and puts it at PATH instead. And PATH must end withCargo.lock, like--lockfile-path /tmp/temporary-lockfile/Cargo.lock. Heads up: passing--lockfile-pathmakes Cargo ignore any existing lockfile at the default path β 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. This is the move when you want to run most commands inside read-only directories and still have the lockfile land somewhere writable. β¨
This option is only available on the nightly channel <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and it needs the -Z unstable-options flag turned on to work (see #14421 <https://github.com/rust-lang/cargo/issues/14421>).
π Common Options π
+toolchain β if Cargo got installed through rustup and the very first argument tocargostarts with+, Cargo reads it as a rustup toolchain name (think+stableor+nightly). The rustup documentation <https://rust-lang.github.io/rustup/overrides.html> has the whole lore on how toolchain overrides work. π--configKEY=VALUE or PATH β overrides a Cargo configuration value. The argument is either TOML syntax inKEY=VALUEform, or a path to an extra configuration file. You can pass 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>.-CPATH β changes the current working directory before running any of the operations you asked for. That affects stuff like where Cargo looks for your project manifest (Cargo.toml) by default, and which directories it searches when discovering.cargo/config.toml. Big one: this option must come before the command name, likecargo -C path/to/my-project build. Order matters, bestie, don't fight it. π
This option is only available on the nightly channel <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and it needs the -Z unstable-options flag turned on to work (see #10098 <https://github.com/rust-lang/cargo/issues/10098>).
-h,--helpβ prints help information. When in doubt, ask. π«Ά-Zflag β the unstable (nightly-only) flags for Cargo. Runcargo -Z helpfor the details. π§
π Miscellaneous Options π
-jN,--jobsN β 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 parallel jobs to the number of logical CPUs plus the value you gave. If you pass the stringdefault, it sets the value back to the defaults. And it should not be0. Like, ever. π«--keep-goingβ builds as many crates in the dependency graph as it possibly can, instead of aborting the whole build the moment one of them fails.
For example, if your current package depends on dependencies fails and works, and one of them fails to build, cargo build -j1 may or may not build the one that succeeds (it depends on which of the two builds Cargo picked to run first). Meanwhile cargo build -j1 --keep-going would definitely run both builds, even if the one it ran first fails. Resilience, periodt. π
--future-incompat-reportβ displays a future-incompat report for any future-incompatible warnings that showed up while this command was running.
See cargo-report(1).
πΈ ENVIRONMENT πΈ
Cargo reads environment variables, and the reference <https://doc.rust-lang.org/cargo/reference/environment-variables.html> has all the details on which ones. Go peek if you're curious, iykyk. π¦
β EXIT STATUS β
0: Cargo succeeded. We love to see it. π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 π
And that's the whole page, bestie β this was CARGO-BUILD(1), the Cargo manual, delivered with love and zero skipped flags. Go build something iconic. π
β¨π«Ά
π¦ where this page comes from
- ubuntu jammy: cargo-1.89 1.89.0+dfsg~24.04-0ubuntu0.22.04.2 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.89 1.89.0+dfsg~24.04-0ubuntu0.24.04.2 Β· 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