CARGO-BUILD(1)

πŸ“¦ version 1.88.0, in ubuntu questing

✨ 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. See cargo-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 --workspace flag (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, --test and --bench flags 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 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. 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 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 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.

  • -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. 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 🧚

  • --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 <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 the release profile. See also the --profile option for choosing a specific profile by name. this is the "make it fast" button, she takes a minute but she delivers ✨
  • --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. 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-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. that's where all your build stuff lives, bestie.
  • --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>. tell me everything, I want the tea πŸ“
  • -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. 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-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.

okay that's the whole first half bestie, more is coming so don't go anywhere πŸ’–

✨ Manifest Options ✨

  • --manifest-path path β€” points Cargo straight at the Cargo.toml file you want, no searching required. Without it, Cargo goes hunting for Cargo.toml in 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 the rust-version spec 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 existing Cargo.lock file 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 --locked and --offline. πŸ“
  • --lockfile-path PATH β€” moves the lockfile away from its default home (<workspace_root>/Cargo.lock) and puts it at PATH instead. And PATH must end with Cargo.lock, like --lockfile-path /tmp/temporary-lockfile/Cargo.lock. Heads up: passing --lockfile-path makes 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 to cargo starts with +, Cargo reads it as a rustup toolchain name (think +stable or +nightly). The rustup documentation <https://rust-lang.github.io/rustup/overrides.html> has the whole lore on how toolchain overrides work. πŸŽ€
  • --config KEY=VALUE or PATH β€” overrides a Cargo configuration value. The argument is either TOML syntax in KEY=VALUE form, 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>.
  • -C PATH β€” 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, like cargo -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. 🫢
  • -Z flag β€” the unstable (nightly-only) flags for Cargo. Run cargo -Z help for the details. 🧚

πŸ’– Miscellaneous Options πŸ’–

  • -j N, --jobs N β€” how many parallel jobs to run. You can also set this with the build.jobs config 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 string default, it sets the value back to the defaults. And it should not be 0. 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 πŸŽ€

  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 CARGO-BUILD(1), the Cargo manual, delivered with love and zero skipped flags. Go build something iconic. πŸ’…βœ¨πŸ«Ά

πŸ“¦ where this page comes from