CARGO-BUILD(1)

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

πŸ’… 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 in cargo-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 --workspace flag, 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, --test and --bench flags 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 the test = true manifest 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 the test flag 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 the bench = true manifest 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 the bench flag 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.

  • -F features, --features features β€” space or comma separated list of features to activate. features of workspace members may be enabled with package-name/feature-name syntax. 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

  • --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 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 the release profile. see also the --profile option for choosing a specific profile by name.
  • --profile name β€” 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; --timings without 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 file cargo-timing.html to the target/cargo-timings directory 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-dir directory β€” 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.
  • --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. πŸ™ˆ

πŸŽ€ 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).
  • -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).
  • --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.
    • never: never display colors.

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 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 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. πŸ’€

✨ Manifest Options ✨

  • --manifest-path path β€” tells Cargo exactly where your Cargo.toml lives. By default Cargo just goes hunting for a Cargo.toml in 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 the rust-version spec 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 existing Cargo.lock file 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 --locked and --offline at the same time. 🧊
  • --lockfile-path PATH β€” changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) over to PATH. PATH must end with Cargo.lock (e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Real talk: providing --lockfile-path will 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 to cargo starts with +, Cargo reads it as a rustup toolchain name (think +stable or +nightly). See the rustup documentation <https://rust-lang.github.io/rustup/overrides.html> for more on how toolchain overrides work.
  • --config KEY=VALUE or PATH β€” overrides a Cargo configuration value. The argument should be in TOML syntax of KEY=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.
  • -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, like cargo -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? 🫢
  • -Z flag β€” unstable (nightly-only) flags to Cargo. Run cargo -Z help for the deets.

πŸ’… 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 β€” 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 πŸŽ€

  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)


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