CARGO-RUSTC(1)

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

✨ NAME ✨

cargo-rustc β€” bestie, this one compiles the current package and lets you yeet extra options straight at the compiler πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

cargo rustc [options] [-- args]

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the vibe: the specified target for the current package (or the package you picked with -p, if you did) gets compiled, along with all of its dependencies. The args you hand over get passed to the final compiler invocation only β€” not to any of the dependencies. And heads up: the compiler is still gonna unconditionally receive arguments like -L, --extern, and --crate-type, and your specified args just get added on top of that. No cap.

For all the tea on rustc flags, go read https://doc.rust-lang.org/rustc/index.html.

This command requires that only one target is being compiled when you're passing additional arguments. If more than one target is available for the current package, you have to use the filters of --lib, --bin, etc, to select which target gets compiled. periodt.

If you want to pass flags to all compiler processes spawned by Cargo, use the RUSTFLAGS environment variable at https://doc.rust-lang.org/cargo/reference/environment-variables.html, or the build.rustflags config value at https://doc.rust-lang.org/cargo/reference/config.html.


πŸ’– OPTIONS πŸ’–

πŸ“¦ Package Selection πŸ“¦

By default, the package in the current working directory is selected. The -p flag can be used to choose a different package in a workspace.

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

Binary targets are automatically built if there is an integration test or benchmark being selected to build. This allows an integration test to execute the binary to exercise and test its behavior. The CARGO_BIN_EXE_<name> environment variable at 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 at https://doc.rust-lang.org/std/macro.env.html to locate the executable. iykyk.

Passing target selection flags will build only the specified targets.

Note that --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. 🫢
  • --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. the whole buffet, bestie πŸ“

🌸 Feature Selection 🌸

The feature flags allow you to control which features are 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.

  • -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. go big or go home.
  • --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.

This may also be specified with the build.target config value at 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. lowkey important, don't sleep on it.
  • -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.

The rustc subcommand will treat the following named profiles with special behaviors:

See the reference at 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.
    • 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 crate-type field in the manifest for configuring a Cargo target. See crate-type field at 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 at 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 at 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 at 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 at 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 β€” tells Cargo not to shove rustc's diagnostics into the JSON messages it prints. Instead, Cargo itself renders the JSON diagnostics coming from rustc. Cargo's own JSON diagnostics and the others coming from rustc are still emitted, don't worry bestie. 🫢 Can't be used with human or short. Nope, pick a lane.

🌸 Manifest Options 🌸

  • --manifest-path path β€” the path to the Cargo.toml file. By default Cargo goes looking for Cargo.toml in the current directory or any parent directory, like a lil detective πŸ•΅οΈβ€β™€οΈ
  • --ignore-rust-version β€” ignores the rust-version specification in packages. Living dangerously, we love that for you.
  • --locked β€” asserts that the exact same dependencies and versions get used as when the existing Cargo.lock file was originally generated. Cargo will 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 basically, CI bestie core.

  • --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 bestie β€” this may result in a 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 cargo-fetch(1) to download dependencies before going offline. Plan ahead, queen. πŸ‘‘

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-for-one, iconic. ✨
  • --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. Mix and match, it's giving versatility. πŸ’…
  • --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. 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 requires the -Z unstable-options flag to enable (see #10098 <https://github.com/rust-lang/cargo/issues/10098>).
  • -h, --help β€” prints help information. Your little rescue blanket when you're lost. 🩹
  • -Z flag β€” unstable (nightly-only) flags to Cargo. Run cargo -Z help for details. Nightly girlies only, sorry to the stable girlies. πŸ’œ

🌈 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. Not zero, bestie, we're begging. 😭
  • --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 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. Deterministic queen behaviour. πŸ‘‘

  • --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. πŸŽ‰ slay!
  • 101: Cargo failed to complete. πŸ’€ it's not giving.

πŸ¦‹ 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 which prints the size of every type:
cargo rustc --lib -- -Z print-type-sizes
  1. Override crate-type field in Cargo.toml with command-line option:
cargo rustc --lib --crate-type lib,cdylib

πŸ‘‘ SEE ALSO πŸ‘‘

cargo(1), cargo-build(1), rustc(1)


And that's a wrap on CARGO-RUSTC(1) bestie!! πŸŽ€ You just read the whole thing, look at you go, main character energy fr fr. Go build something iconic. πŸ’–

πŸ“¦ where this page comes from