CARGO-RUSTC(1)

๐Ÿ“ฆ version 1.78.0, in ubuntu jammy, ubuntu noble

โœจ CARGO-RUSTC โœจ

okay so this is cargo-rustc, and yes bestie, it's a General Commands Manual page (cargo-rustc(1)) โ€” the one that lets you compile the current package and sneak your own extra options straight to the compiler. let's get into it ๐Ÿ’…


๐ŸŒธ NAME ๐ŸŒธ

cargo-rustc โ€” Compile the current package, and pass extra options to the compiler โœจ


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

cargo rustc [options] [-- args]

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So here's the vibe: the specified target for the current package (or the package you named with -p, if you did that) gets compiled along with all of its dependencies. The args you pass get handed to the final compiler invocation โ€” not to any of the dependencies. Important little detail though: the compiler is still unconditionally going to receive arguments like -L, --extern, and --crate-type, and your specified args just get added on top of that compiler invocation. no cap.

Wanna know what all the rustc flags actually do? Go read <https://doc.rust-lang.org/rustc/index.html> for the documentation on rustc flags. ๐Ÿ“š

Heads up bestie โ€” this command requires that only one target is being compiled when you're providing additional arguments. If more than one target is available for the current package, you must use the filters of --lib, --bin, etc, to select which target is compiled. no picking "all of them" here, sorry ๐Ÿ˜ญ

And if your goal is to pass flags to all the compiler processes spawned by Cargo, that's a different thing โ€” use the RUSTFLAGS environment variable <https://doc.rust-lang.org/cargo/reference/environment-variables.html> or the build.rustflags config value <https://doc.rust-lang.org/cargo/reference/config.html>. ๐Ÿซถ


๐Ÿ’– OPTIONS ๐Ÿ’–

๐ŸŽฏ Package Selection

Default behaviour, the classic: the package in the current working directory is selected. But the -p flag exists so you can choose a different package in a workspace. main character energy for that other crate.

  • -p spec, --package spec โ€” The package to build. See cargo-pkgid(1) for the SPEC format. ๐ŸŽ€

๐ŸŒˆ Target Selection

When no target selection options are given, cargo rustc will build all binary and library targets of the selected package. slay.

Binary targets are automatically built if there is an integration test or benchmark being selected to build. This lets an integration test 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> is 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. ๐Ÿฆ‹

Passing target selection flags will build only the specified targets.

glow-up note: --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. protect the globs bestie ๐Ÿ™ˆ
  • --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 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.
  • --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. ๐Ÿ‘‘

๐Ÿงš 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.
  • -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.
  • --ignore-rust-version โ€” Build the target even if the selected Rust compiler is older than the required Rust version as configured in the project's rust-version field. living dangerously, iconic ๐Ÿ’…
  • --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.
  • --crate-type crate-type โ€” Build for the given crate type. This flag accepts a comma-separated list of 1 or more crate types, of which the allowed values are the same as the crate-type field in the manifest for configuring a Cargo target. See crate-type field <https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-crate-type-field> for possible values.
    • If the manifest contains a list, and --crate-type is provided, the command-line argument value will override what is in the manifest.
    • This flag only works when building a lib or example library target. ๐ŸŽ€

๐Ÿ“ฆ 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. โœจ

๐Ÿ‘€ 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 โ€” bestie, this one tells Cargo to NOT stuff rustc's diagnostics into the JSON messages it prints. Instead, Cargo itself renders the JSON diagnostics that come from rustc. Cargo's own JSON diagnostics, plus the other ones coming from rustc, are still emitted, so nothing gets lost. Cannot be used with human or short โ€” it's giving "pick a lane, babe". ๐Ÿšซ

๐ŸŒธ Manifest Options ๐ŸŒธ

  • --manifest-path path โ€” the path to the Cargo.toml file, obviously ๐Ÿ’…. By default Cargo goes hunting for Cargo.toml in the current directory or any parent directory, like a little detective. ๐Ÿ”
  • --frozen, --locked โ€” either one of these flags demands that the Cargo.lock file is up-to-date. If the lock file is missing, or it needs to be updated, Cargo will exit with an error. No negotiating. ๐Ÿ˜ค The --frozen flag also stops Cargo from even trying to reach the network to figure out if it's out-of-date.

These are for environments where you want to assert that the Cargo.lock file is up-to-date (like a CI build), or where you just want to avoid network access entirely. Very "trust nothing, verify everything" of you. โœจ

  • --offline โ€” prevents Cargo from accessing the network for any reason. Without this flag, Cargo will stop 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. Girl is resourceful. ๐Ÿ’โ€โ™€๏ธ
โš ๏ธ Beware: 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. Do your prep work first, babe.

May also be specified with the net.offline config value (https://doc.rust-lang.org/cargo/reference/config.html). ๐Ÿฆ‹


๐Ÿ‘‘ Common Options ๐Ÿ‘‘

  • +toolchain โ€” if Cargo was installed with rustup, and the first argument to cargo begins with +, it gets interpreted as a rustup toolchain name (like +stable or +nightly). See the rustup documentation (https://rust-lang.github.io/rustup/overrides.html) for more info on how toolchain overrides work. Switching personas mid-command, iconic. ๐ŸŽ€
  • --config KEY=VALUE or PATH โ€” overrides a Cargo configuration value. The argument should be in TOML syntax of KEY=VALUE, or given as a path to an extra configuration file. This flag may be specified multiple times โ€” stack 'em, queen. See the command-line overrides section (https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides) for more info. ๐Ÿ“
  • -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), and also 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, it's not a vibe thing, it's a rule. ๐Ÿ™ˆ
๐ŸŒ™ 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). Nightly girlies only. ๐Ÿ’œ
  • -h, --help โ€” prints help information. The classic. When in doubt, ask for help. ๐Ÿซถ
  • -Z flag โ€” unstable (nightly-only) flags for Cargo. Run cargo -Z help for the 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 the provided value. If a string default is provided, it sets the value back to defaults. Should not be 0. Zero is not the move. ๐Ÿ’€
  • --keep-going โ€” build as many crates in the dependency graph as possible, rather than aborting the whole build on the first one that fails. Resilience! Main character energy! ๐Ÿ”ฅ

For example, if the current package depends on dependencies fails and works, one of which fails to build, cargo rustc -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 rustc -j1 --keep-going would definitely run both builds, even if the one run first fails. Consistent behaviour? We love to see it. โœจ

  • --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. Go read it, bestie, it's all there. ๐Ÿ“–


๐Ÿ’ซ EXIT STATUS ๐Ÿ’ซ

  • 0: Cargo succeeded. ๐ŸŽ‰ slay.
  • 101: Cargo failed to complete. ๐Ÿ˜ญ we've all been there.

๐ŸŽ€ EXAMPLES ๐ŸŽ€

  1. Check if your package (not including dependencies) uses unsafe code:
cargo rustc --lib -- -D unsafe-code
  1. Try an experimental flag on the nightly compiler, such as this one which prints the size of every type:
cargo rustc --lib -- -Z print-type-sizes
  1. Override the crate-type field in Cargo.toml with a command-line option:
cargo rustc --lib --crate-type lib,cdylib

๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

cargo(1), cargo-build(1), rustc(1) โ€” go read those too, they're the whole friend group. ๐Ÿ’–


And that's a wrap on this one, bestie!! ๐Ÿ’… This was the CARGO-RUSTC(1) page โ€” that's the name sitting pretty in the footer, and yes it's also the source and version tag of this very doc, so now you know exactly which page you just devoured. Go forth and cargo rustc responsibly. ๐Ÿซถโœจ๐ŸŒธ

๐Ÿ“ฆ where this page comes from