CARGO-BUILD(1)

πŸ“¦ version 1.82.0, in ubuntu jammy, ubuntu noble

πŸ’– 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 in cargo-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 --workspace flag, 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, --test and --bench flags 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 the test = true manifest 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 the test flag 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 the bench = true manifest 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 the bench flag 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 the package-name/feature-name syntax. 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>. run rustc --print target-list for 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 the release profile. also peep the --profile option 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; --timings with 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 file cargo-timing.html to the target/cargo-timings directory 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 the CARGO_TARGET_DIR environment variable, or the build.target-dir config value (https://doc.rust-lang.org/cargo/reference/config.html). defaults to target in the root of the workspace. yes, that target folder. 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-options flag to enable. see https://github.com/rust-lang/cargo/issues/6790 for 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 the term.verbose config value (https://doc.rust-lang.org/cargo/reference/config.html). the way that double -v just unlocks a whole extra layer of information 😭
  • -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. 🌸
  • --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 with short and json.
    • short: emit shorter, human-readable text messages. conflicts with human and json.
    • 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 with human and short.
    • json-diagnostic-short: ensure the rendered field of JSON messages contains the "short" rendering from rustc. cannot be used with human or short.
    • json-diagnostic-rendered-ansi: ensure the rendered field of JSON messages contains embedded ANSI color codes, for respecting rustc's default color scheme. cannot be used with human or short.
    • json-render-diagnostics: instruct Cargo to not include rustc diagnostics in JSON messages printed, but instead have Cargo itself render the JSON diagnostics coming from rustc. Cargo's own JSON diagnostics and others coming from rustc are still emitted. cannot be used with human or short.
  • --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-options flag to enable. see https://github.com/rust-lang/cargo/issues/5579 for more information. nightly girlies only. ✨

πŸŽ€ Manifest Options πŸŽ€

  • --manifest-path path β€” where ur Cargo.toml lives, bestie. By default Cargo goes hunting for Cargo.toml in the current directory or any parent directory, but this lets u just point at it directly.
  • --ignore-rust-version β€” ignore the rust-version specification 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 existing Cargo.lock file 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 --locked and --offline. two flags in a trench coat, slay.
  • --lockfile-path PATH β€” changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock (e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that providing --lockfile-path will 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 to cargo begins with +, it will be interpreted as a rustup toolchain name (such as +stable or +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
  • --config KEY=VALUE or PATH β€” overrides a Cargo configuration value. The argument should be in TOML syntax of KEY=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.
  • -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), as well as the directories searched for discovering .cargo/config.toml, for example. This option must appear before the command name, for example cargo -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-options flag 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. πŸ’…
  • -Z flag β€” unstable (nightly-only) flags to Cargo. Run cargo -Z help for details. nightly girlies only

🌈 Miscellaneous Options 🌈

  • -j N, --jobs N β€” number of parallel jobs to run. May also be specified with the build.jobs config 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 string default is 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 ate
  • 101: Cargo failed to complete. 😭 it's giving tragedy

⭐ EXAMPLES ⭐

  1. Build the local package and all of its dependencies:
cargo build
  1. 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