CARGO-RUSTC(1)
โจ 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.
-pspec,--packagespec โ The package to build. Seecargo-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,--testand--benchflags 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. ๐--binnameโฆ โ Build the specified binary. This flag may be specified multiple times and supports common Unix glob patterns.--binsโ Build all binary targets. ๐ฅ--examplenameโฆ โ Build the specified example. This flag may be specified multiple times and supports common Unix glob patterns.--examplesโ Build all example targets. โจ--testnameโฆ โ 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 thetest = truemanifest 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
testflag in the manifest settings for the target.
--benchnameโฆ โ 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 thebench = truemanifest 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
benchflag 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. ๐
-Ffeatures,--featuresfeatures โ Space or comma separated list of features to activate. Features of workspace members may be enabled withpackage-name/feature-namesyntax. 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
--targettriple โ Build for the given architecture. The default is the host architecture. The general format of the triple is<arch><sub>-<vendor>-<sys>-<abi>. Runrustc --print target-listfor a list of supported targets. This flag may be specified multiple times.- This may also be specified with the
build.targetconfig 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/guide/build-cache.html> documentation for more details.
- This may also be specified with the
-r,--releaseโ Build optimized artifacts with thereleaseprofile. See also the--profileoption for choosing a specific profile by name. ๐ฅ--profilename โ Build with the given profile.- The
rustcsubcommand will treat the following named profiles with special behaviors: checkโ Builds in the same way as thecargo-check(1)command with thedevprofile.testโ Builds in the same way as thecargo-test(1)command, enabling building in test mode which will enable tests and enable thetestcfg option. See rustc tests <https://doc.rust-lang.org/rustc/tests/index.html> for more detail.benchโ Builds in the same was as thecargo-bench(1)command, similar to thetestprofile.- See the reference <https://doc.rust-lang.org/cargo/reference/profiles.html> for more details on profiles.
- The
--ignore-rust-versionโ Build the target even if the selected Rust compiler is older than the required Rust version as configured in the project'srust-versionfield. 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;--timingswithout 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 filecargo-timing.htmlto thetarget/cargo-timingsdirectory 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-typecrate-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 thecrate-typefield 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-typeis provided, the command-line argument value will override what is in the manifest. - This flag only works when building a
liborexamplelibrary target. ๐
- If the manifest contains a list, and
๐ฆ Output Options
--target-dirdirectory โ Directory for all generated artifacts and intermediate files. May also be specified with theCARGO_TARGET_DIRenvironment variable, or thebuild.target-dirconfig value <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults totargetin 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 theterm.verboseconfig value <https://doc.rust-lang.org/cargo/reference/config.html>. ๐ฃ๏ธ-q,--quietโ Do not print cargo log messages. May also be specified with theterm.quietconfig value <https://doc.rust-lang.org/cargo/reference/config.html>. ๐คซ--colorwhen โ 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.colorconfig value <https://doc.rust-lang.org/cargo/reference/config.html>.
--message-formatfmt โ 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 withshortandjson.short: Emit shorter, human-readable text messages. Conflicts withhumanandjson.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 withhumanandshort.json-diagnostic-short: Ensure therenderedfield of JSON messages contains the "short" rendering from rustc. Cannot be used withhumanorshort.json-diagnostic-rendered-ansi: Ensure therenderedfield of JSON messages contains embedded ANSI color codes for respecting rustc's default color scheme. Cannot be used withhumanorshort.
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 withhumanorshortโ it's giving "pick a lane, babe". ๐ซ
๐ธ Manifest Options ๐ธ
--manifest-pathpath โ the path to theCargo.tomlfile, obviously ๐ . By default Cargo goes hunting forCargo.tomlin the current directory or any parent directory, like a little detective. ๐--frozen,--lockedโ either one of these flags demands that theCargo.lockfile 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--frozenflag 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 tocargobegins with+, it gets interpreted as a rustup toolchain name (like+stableor+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=VALUEorPATHโ overrides a Cargo configuration value. The argument should be in TOML syntax ofKEY=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. ๐-CPATH โ changes the current working directory before executing any specified operations. This affects things like wherecargolooks 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 examplecargo -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-optionsflag 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. ๐ซถ-Zflag โ unstable (nightly-only) flags for Cargo. Runcargo -Z helpfor the details. โจ
๐ Miscellaneous Options ๐
-jN,--jobsN โ number of parallel jobs to run. May also be specified with thebuild.jobsconfig 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 stringdefaultis provided, it sets the value back to defaults. Should not be0. 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 ๐
- Check if your package (not including dependencies) uses unsafe code:
cargo rustc --lib -- -D unsafe-code
- 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
- Override the
crate-typefield inCargo.tomlwith 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
- ubuntu jammy: cargo-1.78 1.78.0+dfsg1ubuntu1~bpo0-0ubuntu0.22.04.1 ยท Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.78 1.78.0+dfsg1ubuntu1-0ubuntu0.24.04.2 ยท MIT OR Apache-2.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms