CARGO-BUILD(1)

๐Ÿ’– CARGO-BUILD โ€” the manual page, but make it make sense

okay bestie, so this is the page for cargo-build, straight out of the General Commands Manual (yes, section 1, the cool section). It's long, so buckle up โ€” part 1 starts now and there's more coming after this. ๐ŸŽ€


โœจ NAME โœจ

cargo-build โ€” Compile the current package ๐Ÿฆ‹


๐ŸŒธ SYNOPSIS ๐ŸŒธ

cargo build [options]

that's it. that's the whole vibe. ๐Ÿซถ


๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

Compile local packages and all of their dependencies. no cap, that's literally the whole job. ๐Ÿ’…


๐Ÿ‘‘ OPTIONS ๐Ÿ‘‘

๐ŸŒˆ Package Selection

okay so here's the tea on how Cargo decides which packages it's even gonna look at when you don't say anything: by default, with no package selection options given, the packages selected depend on the selected manifest file โ€” and that manifest is picked based on the current working directory, unless you passed --manifest-path. ๐Ÿงš

If that manifest is the root of a workspace, then the workspace's default members get selected. Otherwise? Only the single package defined by that manifest. That's it, that's the rule. โœจ

You can set a workspace's default members explicitly with the workspace.default-members key in the root manifest. And if you don't set it:

  • a virtual workspace will include all workspace members (equivalent to passing --workspace) ๐Ÿ‘‘
  • a non-virtual workspace will include only the root crate itself ๐Ÿ’€
  • -p specโ€ฆ, --package specโ€ฆ โ€” Build only the specified packages. Wanna know what the SPEC format looks like? Peep cargo-pkgid(1). This flag may be specified multiple times and supports common Unix glob patterns like *, ? and []. BUT โ€” and this part matters โ€” to avoid your shell accidentally expanding those glob patterns before Cargo even gets to handle them, you must wrap each pattern in single quotes or double quotes. quoted, bestie. always. ๐Ÿ™ˆ
  • --workspace โ€” Build all members in the workspace. everybody's invited. ๐ŸŽ‰
  • --all โ€” Deprecated alias for --workspace. it's giving legacy. use the new one. ๐Ÿ’…
  • --exclude *SPEC*โ€ฆ โ€” Exclude the specified packages. Must be used in conjunction with the --workspace flag. This flag may be specified multiple times and supports common Unix glob patterns like *, ? and []. However โ€” same deal 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 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. sorry to that binary. ๐Ÿ˜ญ

Binary targets are automatically built if there's an integration test or benchmark being selected to build โ€” this lets an integration test actually execute the binary so it can exercise and test its behavior. The CARGO_BIN_EXE_<name> environment variable (see https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates) is set when the integration test is built and run, so it can use the env macro (https://doc.rust-lang.org/std/macro.env.html) or the var function (https://doc.rust-lang.org/std/env/fn.var.html) to locate the executable. iconic behavior honestly. ๐Ÿ‘‘

Passing target selection flags will build only the specified targets.

heads up bestie: --bin, --example, --test and --bench 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. ๐Ÿซถ
  • --lib โ€” Build the package's library. the classic. ๐Ÿ’œ
  • --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 that have the test = true manifest 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 the test flag in the manifest settings for the target. double duty, no complaints. ๐Ÿ˜ญ
  • --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 the bench = true manifest 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 the bench flag in the manifest settings for the target. same energy as --tests, fr fr. ๐Ÿ”ฅ
  • --all-targets โ€” Build all targets. This is equivalent to specifying --lib --bins --tests --benches --examples. the "select all" button but for builds. ๐Ÿ‘‘

๐ŸŒธ Feature Selection

Feature flags are how you control which features get enabled. When no feature options are given, the default feature is activated for every selected package. โœจ

See the features documentation at https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options for more details. iykyk. ๐Ÿ’œ

  • -F features, --features features โ€” Space or comma separated list of features to activate. Features of workspace members may be enabled with the package-name/feature-name syntax. 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. go big or go home. ๐ŸŒˆ
  • --no-default-features โ€” Do not activate the default feature of the selected packages. sometimes you gotta opt out. ๐Ÿ’…

๐Ÿ”ฅ Compilation Options

  • --target triple โ€” Build for the specified target architecture. The flag may be specified multiple times. The default is the host architecture. The general format of the triple is <arch><sub>-<vendor>-<sys>-<abi>. ๐Ÿฆ‹

Possible values:

  • Any supported target in rustc --print target-list.
  • "host-tuple", which will internally be substituted by the host's target. This can be particularly useful if you're cross-compiling some crates, and don't want to specify your host's machine as a target (for instance, an xtask in a shared project that may be worked on by many hosts). โœจ
  • A path to a custom target specification. See Custom Target Lookup Path at https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path for more information.

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 at https://doc.rust-lang.org/cargo/reference/build-cache.html for more details. things get relocated, it's fine, it's fine. ๐Ÿ™ˆ
  • -r, --release โ€” Build optimized artifacts with the release profile. See also the --profile option for choosing a specific profile by name. optimized and thriving. ๐Ÿ’…
  • --profile name โ€” Build with the given profile. See the reference at https://doc.rust-lang.org/cargo/reference/profiles.html for more details on profiles. pick your fighter. ๐Ÿ‘‘
  • --timings โ€” Output information about how long each compilation takes, and track concurrency information over time. โฑ๏ธ

A file cargo-timing.html will be written to the target/cargo-timings directory at the end of the build. An additional report with a timestamp in its filename is also written if you want to look at a previous run. These reports are suitable for human consumption only, and do not provide machine-readable timing data. sorry robots. ๐Ÿค–๐Ÿ’€


๐Ÿ’œ 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. nightly girlies only, 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). more words, more lore. ๐Ÿ—ฃ๏ธ
  • -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). shhh. ๐Ÿคซ
  • --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 at 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. ๐Ÿ‘‘

๐ŸŽ€ Manifest Options

  • --manifest-path path โ€” Path to the Cargo.toml file. By default, Cargo searches for the Cargo.toml file in the current directory or any parent directory. it'll find it, don't worry. ๐Ÿ”
  • --ignore-rust-version โ€” Ignore rust-version specification in packages. living dangerously, respectfully. ๐Ÿ’…
  • --locked โ€” bestie, this one is giving deterministic vibes only. It asserts that the exact same dependencies and versions are used as when the existing Cargo.lock file was originally generated. Cargo will literally exit with an error when either of these 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. So if you want your builds to be as predictable as your fave playlist, this is it. โœจ

  • --offline โ€” okay so this one is for when you wanna be unbothered by the network. It 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 bestie! 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. So you can set it and forget it. ๐Ÿ’…

  • --frozen โ€” it's giving two for one special. Equivalent to specifying both --locked and --offline. Slay. โ„๏ธ

๐ŸŒˆ 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 information about how toolchain overrides work. So you can switch toolchains like you switch outfits. ๐Ÿ‘—
  • --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. So you can be like, actually, I'll take it from here. ๐Ÿ’โ€โ™€๏ธ
  • -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. ๐Ÿ—‚๏ธ
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). So it's a little exclusive, you know? ๐Ÿ’…
  • -h, --help โ€” prints help information. Because sometimes you just need a little guidance, bestie. ๐Ÿ™
  • -Z flag โ€” unstable (nightly-only) flags to Cargo. Run cargo -Z help for details. It's giving secret menu. ๐Ÿคซ

โœจ 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 provided value. If a string default is provided, it sets the value back to defaults. Should not be 0. So don't be a zero, okay? ๐Ÿ˜‰
  • --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. So it's like, we're not giving up, we're going to see this through. ๐Ÿ’ช

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. So it's the never give up energy. โœจ

  • --future-incompat-report โ€” displays a future-incompat report for any future-incompatible warnings produced during execution of this command. See cargo-report(1). So you can stay ahead of the drama. ๐Ÿ”ฎ

๐ŸŒ ENVIRONMENT ๐ŸŒ

See the reference https://doc.rust-lang.org/cargo/reference/environment-variables.html for details on environment variables that Cargo reads. So many env vars, so little time. ๐Ÿ’…


๐Ÿ’ฏ EXIT STATUS ๐Ÿ’ฏ

  • 0: Cargo succeeded. ๐ŸŽ‰
  • 101: Cargo failed to complete. ๐Ÿ˜ญ

So if you see 101, you know it's not it. ๐Ÿ’€


โœจ EXAMPLES โœจ

  1. Build the local package and all of its dependencies:
cargo build
  1. Build with optimizations:
cargo build --release

So simple, so iconic. ๐Ÿซถ


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

cargo(1), cargo-rustc(1)

Go check 'em out, bestie. They're part of the squad. ๐Ÿ’–


And that's the page! This is from the Cargo manual, and the footer says CARGO-BUILD(1) โ€” iconic. No date given, but you know it's fresh. Stay slay, bestie! ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from