CARGO-BUILD(1)
π cargo-build β the manual, but make it make sense
okay bestie, so this is the cargo-build manual page (general commands manual, if we're being formal π) and it's literally about compiling your current package. it's giving "the thing you type 900 times a day". let's go π¦
β¨ 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. it builds your stuff and everything your stuff needs to run π«Ά
π OPTIONS π
π Package Selection π
so by default, when you don't pass any package selection options, which packages get selected depends on the selected manifest file β and that's based on the current working directory, unless you gave it --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 gets selected. simple as that, no cap β¨
and 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? then 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. peepcargo-pkgid(1)for the SPEC format, bestie. this flag can be specified multiple times and it supports common Unix glob patterns like*,?and[]. BUT β and this is the part everyone gets wrong β to avoid your shell accidentally expanding glob patterns before Cargo even gets its hands on them, you must use single quotes or double quotes around each pattern. periodt π--workspaceβ build all members in the workspace. the whole squad π₯--allβ deprecated alias for--workspace. it's giving legacy, we don't use it anymore π--exclude SPECβ¦β exclude the specified packages. must be used in conjunction with the--workspaceflag (fr fr, they're a package deal). this flag may be specified multiple times and supports 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 pattern. same rule as always π
π¦ Target Selection π¦
when no target selection options are given, 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 felicia βοΈ
binary targets are automatically built if there's an integration test or benchmark being selected to build. this is so an integration test can execute the binary to exercise and test its behavior β iconic design 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. so cute, so functional π
passing target selection flags will build only the specified targets.
heads up bestie:--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. i'm not repeating myself, i'm just quoting the manual π
--libβ build the package's library. the classics π--bin nameβ¦β build the specified binary. this flag may be specified multiple times and supports common Unix glob patterns.--binsβ build all binary targets. all of them. every single one π«Ά--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 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 fun π--bench nameβ¦β build the specified benchmark. this flag may be specified multiple times and 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, 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β build all targets. this is equivalent to specifying--lib --bins --tests --benches --examples. the everything bagel of flags π₯―β¨
π 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 default behavior, and yes it's a little confusing that "default" is also a feature name. we don't make the rules π€·
see the features documentation (https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options) for more details, bestie.
-F features,--features featuresβ 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. slay π--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 minimal today β¨
β 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 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. yourtarget/folder is about to get a whole new subfolder bestie π
-r,--releaseβ build optimized artifacts with thereleaseprofile. see also the--profileoption for choosing a specific profile by name. this is the "i want it fast at runtime and i'm willing to wait now" button π₯--profile nameβ build with the given profile. see the reference (https://doc.rust-lang.org/cargo/reference/profiles.html) for more details on profiles. custom profiles are so main character π--timings=fmtsβ output information about 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. it's pretty tho β¨json(unstable, requires-Zunstable-options): emit machine-readable JSON information about timing information. for the robots π€
π Output Options π
--target-dir directoryβ directory for all generated artifacts and intermediate files. may also be specified with theCARGO_TARGET_DIRenvironment variable, or the build.target-dir config value (https://doc.rust-lang.org/cargo/reference/config.html). defaults totargetin the root of the workspace. your build output's home address π--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. nightlies only, bestie, don't try this on stable π
π§ 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 the term.verbose config value (https://doc.rust-lang.org/cargo/reference/config.html). -vv is where the lore drops π-q,--quietβ do not print cargo log messages. may also be specified with the term.quiet config value (https://doc.rust-lang.org/cargo/reference/config.html). silence. peace. tranquility π§--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, no matter what πnever: never display colors. for the minimalists.
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 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. it's giving "cargo takes the wheel" π
--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. peek behind the curtain, but only on nightly π
okay that's part one bestie, there's more coming so don't go anywhere π
β¨ Manifest Options β¨
Okay bestie, these are the manifest-flavored knobs. π
--manifest-pathpath β points Cargo at theCargo.tomlfile you want. By default, Cargo searches for theCargo.tomlfile in the current directory or any parent directory. π«Ά--ignore-rust-versionβ ignorerust-versionspecification in packages. like, pretend it's not even there. π--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. slay for reproducibility. β¨
--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. π¦
Beware that this may 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 as indicated in the local copy of the index. See the cargo-fetch(1) command to download dependencies before going offline. π₯
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 moods in one. π--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-options flag 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 information about how toolchain overrides work. π--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. π§
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 classic. π-Zflag β unstable (nightly-only) flags to Cargo. Runcargo -Z helpfor details. π
β¨ 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 provided value. If a string default is provided, it sets the value back to defaults. Should not be0. π--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. π₯
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. πΈ
β¨ EXIT STATUS β¨
0: Cargo succeeded. π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 β¨
cargo(1), cargo-rustc(1) π«Ά
And that's the whole vibe, bestie β this was CARGO-BUILD(1), source/version and date straight from the footer. Go build something iconic. Periodt. π
β¨
π¦ where this page comes from
- ubuntu jammy: cargo-1.83 1.83.0+dfsg0ubuntu2~bpo2-0ubuntu2.22.04.1 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.83 1.83.0+dfsg0ubuntu1~bpo2-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