CARGO-RUSTC(1)

πŸ“¦ version 1.88.0, in ubuntu questing

πŸ’… CARGO-RUSTC(1) πŸ’…

okay bestie, sit down, hydrate, because today we're learning cargo-rustc β€” yes it's the cargo rustc subcommand, the manual page is just named like that, that's just how cargo names its pages, iykyk πŸ’–


✨ NAME ✨

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

no cap, that's the whole vibe: build your crate, and then whisper sweet extra flags into the compiler's ear πŸ¦‹


🌸 SYNOPSIS 🌸

cargo rustc [options] [-- args]

πŸ’– DESCRIPTION πŸ’–

So here's the tea β˜•. The target you specify for the current package β€” or for the package you named with -p, if you did that β€” gets compiled, and so do all of its dependencies. Like, the whole friend group comes along.

But here's the important part: those args you put after the --? They get passed to the final compiler invocation only, not to any of the dependencies. Your deps don't get to hear your little secrets πŸ™ˆ.

Also, heads up: the compiler is still gonna unconditionally receive arguments like -L, --extern, and --crate-type no matter what. Your specified args just get added on top of those. You're not replacing anything, you're just adding to the pile. Stack it up, queen πŸ‘‘

For the full rundown on rustc flags, go read <https://doc.rust-lang.org/rustc/index.html>.

Now, the drama: this command requires that only one target is being compiled when you're passing extra arguments. So if your current package has more than one target available, you must use the filters β€” --lib, --bin, etc. β€” to pick which one actually gets compiled. No picking favorites by accident, okay? 😭

And if what you actually want is to pass flags to all the compiler processes Cargo spawns, not just this one? Then 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>. Different tool, different job, both iconic πŸ’…


πŸ‘‘ OPTIONS πŸ‘‘

πŸŽ€ Package Selection πŸŽ€

By default, the package sitting in your current working directory is the one that gets picked. If you wanna choose a different package inside a workspace, that's what the -p flag is for. Main character selection, honestly.

  • -p spec, --package spec β€” The package to build. For the whole SPEC format situation, see cargo-pkgid(1).

🧚 Target Selection 🧚

When you don't give any target selection options at all, cargo rustc will build all the binary and library targets of the package you selected. Everything. The whole squad 🌈

Binary targets get automatically built if there's an integration test or benchmark being selected to build. Why? So an integration test can actually execute the binary and exercise it / test how it behaves. 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 the test can use the env macro <https://doc.rust-lang.org/std/macro.env.html> to locate the executable. Cute, right? The test knows exactly where its little binary lives πŸ₯Ί

Passing target selection flags means you build only the targets you named. No extras.

Heads up bestie: --bin, --example, --test and --bench also support common Unix glob patterns like *, ? and []. BUT β€” to stop your shell from accidentally expanding those glob patterns before Cargo ever gets to handle them, you must wrap each glob pattern in single quotes or double quotes. Quote your globs, protect your peace ✨
  • --lib β€” Build the package's library.
  • --bin *name*… β€” Build the specified binary. You can specify this flag multiple times, and it supports common Unix glob patterns.
  • --bins β€” Build all binary targets.
  • --example *name*… β€” Build the specified example. You can specify this flag multiple times, and it supports common Unix glob patterns.
  • --examples β€” Build all example targets.
  • --test *name*… β€” Build the specified integration test. You can specify this flag multiple times, and it 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 get 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. Yes, double building, it's giving overachiever πŸ’€
  • --bench *name*… β€” Build the specified benchmark. You can specify this flag multiple times, and it 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 get 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 "yes to everything" button 🫢

πŸ’œ Feature Selection πŸ’œ

Feature flags are how you control which features are enabled. When you give no feature options at all, the default feature gets activated for every selected package. That's the baseline, the default setting, the factory reset of features.

More deets live in the features documentation <https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options>.

  • -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, babe πŸ“
  • --all-features β€” Activate all available features of all selected packages. ALL of them. Go feral.
  • --no-default-features β€” Do not activate the default feature of the selected packages. Sorry default feature, not 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 documentation <https://doc.rust-lang.org/cargo/reference/build-cache.html> for more details. Different directory, different era, it's giving relocation era 😌
  • -r, --release β€” Build optimized artifacts with the release profile. See also the --profile option for choosing a specific profile by name. The classic "ship it" button.
  • --profile name β€” Build with the given profile.

The rustc subcommand treats these following named profiles with special behaviors:

See the reference <https://doc.rust-lang.org/cargo/reference/profiles.html> for more details on profiles. (And yes, "same was" is exactly what the original says β€” I'm not fixing it, I'm just the messenger 🧚)

  • --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. For the girlies who love a visual πŸŽ€
    • json (unstable, requires -Zunstable-options): Emit machine-readable JSON information about timing information. For the robots, the pipelines, the automation girlies πŸ€–
  • --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 the 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. Command line wins, manifest takes the L.

This flag only works when building a lib or example library target. That's it, that's the limit.

🌟 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. Home sweet home 🏠

⭐ 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>. Double it and the tea gets detailed.
  • -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, gorgeous. The quiet luxury aesthetic 🀫
  • --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>. Rainbow or no rainbow, your call 🌈

  • --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's a little switcheroo πŸ’… it tells Cargo to not stuff rustc diagnostics into the JSON messages it prints, and instead have Cargo itself render the JSON diagnostics that rustc sends over. Cargo's own JSON diagnostics, and the others coming from rustc, are still emitted either way. Cannot be used with human or short. No mixing, periodt.

πŸŽ€ Manifest Options πŸŽ€

  • --manifest-path path β€” where the Cargo.toml file lives. By default Cargo goes hunting for Cargo.toml in the current directory, or any parent directory above it. πŸ¦‹
  • --ignore-rust-version β€” straight up ignore the rust-version specification in packages. Not me pretending that field doesn't exist πŸ™ˆ
  • --locked β€” asserts 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 when either of these scenarios pops up:
    • The lock file is missing.
    • Cargo attempted to change the lock file because of a different dependency resolution.

Use it in environments where you want deterministic builds, like in CI pipelines. Very much the "we are NOT improvising today" flag. ✨

  • --offline β€” stops Cargo from touching the network for any reason. Without this flag, Cargo just stops 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. 🧚
Careful, babe: this can give you a different dependency resolution than online mode. Cargo restricts itself to crates that are downloaded locally, even if a newer version exists as indicated in the local copy of the index. See cargo-fetch(1) to download your 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 β€” equivalent to specifying both --locked and --offline at once. Two for one, iconic. πŸ‘‘
  • --lockfile-path PATH β€” moves the path of the lockfile from the default (<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock (like --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that handing over --lockfile-path will ignore the existing lockfile at the default path, and instead either use the lockfile from PATH, or write a brand new lockfile into the provided PATH if it doesn't exist yet. This flag lets you 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 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. πŸ¦‹
  • --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. 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 stuff like where cargo looks by default for the project manifest (Cargo.toml), and 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
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 "help me help me" move 🫢
  • -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 the provided value. If a string default is provided, it sets the value back to defaults. Should not be 0. Just don't. 😭
  • --keep-going β€” build as many crates in the dependency graph as possible, instead of aborting the build on the first one that fails to build. Main character energy for your dependency graph fr fr.

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

  • --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. Yes bestie, Cargo be reading things. πŸ‘€


⭐ EXIT STATUS ⭐

  • 0: Cargo succeeded. πŸŽ‰
  • 101: Cargo failed to complete. πŸ’€

πŸŽ€ 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)


And that's a wrap, bestie 🫢 this page is CARGO-RUSTC(1), straight from the Cargo source (version as shipped in the Cargo distribution) and dated per that footer β€” go forth and cargo rustc responsibly, mwah πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from