CARGO-BUILD(1)

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

πŸ’… 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. peep cargo-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 --workspace flag (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, --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. 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 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 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 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.
  • --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 with package-name/feature-name syntax. 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>. 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. your target/ folder is about to get a whole new subfolder bestie πŸ™ˆ
  • -r, --release β€” build optimized artifacts with the release profile. see also the --profile option 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; --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. 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 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. 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-options flag to enable. see https://github.com/rust-lang/cargo/issues/6790 for 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 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. 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-options flag to enable. see https://github.com/rust-lang/cargo/issues/5579 for 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-path path β€” points Cargo at the Cargo.toml file you want. By default, Cargo searches for the Cargo.toml file in the current directory or any parent directory. 🫢
  • --ignore-rust-version β€” ignore rust-version specification in packages. like, pretend it's not even there. πŸ™ˆ
  • --locked β€” asserts that the exact same dependencies and versions are used as when the existing Cargo.lock file 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 --locked and --offline. two moods in one. πŸ’…
  • --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 information about 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 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. 🧚

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. πŸ’…
  • -Z flag β€” unstable (nightly-only) flags to Cargo. Run cargo -Z help for details. πŸ‘‘

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

  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 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