CARGO-RUSTC(1)
π CARGO-RUSTC π
okay bestie, welcome to cargo-rustc(1) β it's a General Commands Manual page and it's giving "compile my package but also lemme whisper sweet flags to rustc". 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. no target left behind, fr fr.
now the cute part β those args you pass? they all go to the final compiler invocation, and not to any of the dependencies. only the last one gets to hear your little secrets. π
but lowkey important: the compiler is still gonna unconditionally receive arguments like-L,--extern, and--crate-typewhether you like it or not β yourargsjust get tacked on top of the invocation. you're adding, not replacing. periodt.
for the full tea on rustc flags, go look at https://doc.rust-lang.org/rustc/index.html β¨
okay and this one's a whole rule: this command requires that only one target is being compiled when you provide additional arguments. so if your current package has more than one target available, you must use the filters β --lib, --bin, etc. β to pick which target gets compiled. no being wishy-washy about it. π
and if what you actually want is to pass flags to every single compiler process Cargo spawns, that's a different girl β 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). iykyk π
π OPTIONS π
π Package Selection
by default, the package sitting in the current working directory is the one that gets selected. you can use the -p flag to choose a different package inside a workspace. main character energy, choose your fighter. β¨
-pspec,--packagespec β the package to build. check outcargo-pkgid(1) for the SPEC format π¦
π¦ Target Selection
when you give it no target selection options at all, cargo rustc will build all binary and library targets of the selected package. all of 'em. π
binary targets get automatically built if there's an integration test or benchmark being selected to build. this lets an integration test actually run the binary to exercise and test its behavior β iconic. 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. very demure, very mindful. π§
passing target selection flags will build only the specified targets. narrowing it down, love that for us.
heads up bestie:--bin,--example,--testand--benchflags also support common Unix glob patterns like*,?and[]. BUT β to avoid your shell accidentally expanding those glob patterns before Cargo gets its hands on them, you must wrap each glob pattern in single quotes or double quotes. no cap. π₯
--libβ build the package's library π--binnameβ¦ β build the specified binary. this flag can be specified multiple times and supports common Unix glob patterns β¨--binsβ build all binary targets π―--examplenameβ¦ β build the specified example. may be specified multiple times, supports common Unix glob patterns π«--examplesβ build all example targets π--testnameβ¦ β build the specified integration test. may be specified multiple times, 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, plus integration tests. β οΈ be aware 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 thetestflag in the manifest settings for the target.--benchnameβ¦ β build the specified benchmark. may be specified multiple times, 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, plus bench targets. β οΈ same deal β 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 thebenchflag in the manifest settings for the target.--all-targetsβ build all targets. this is equivalent to specifying--lib --bins --tests --benches --examples. literally all of them, go off π
πΈ 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. cute defaults. π
for more details, see the features documentation: https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options
-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. stack 'em up π--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 thebuild.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 documentation (https://doc.rust-lang.org/cargo/guide/build-cache.html) for more details. π
-r,--releaseβ build optimized artifacts with the release profile. see also the--profileoption for choosing a specific profile by name. glow up your binaries β¨--profilename β build with the given profile. π therustcsubcommand will treat the following named profiles with special behaviors:checkβ builds in the same way as thecargo-check(1) command with the dev profile.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 the test profile. (yes bestie, "same was" β we keep the original's typo, that's the law π«‘) see the reference (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;--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. the CLI wins, always. π this flag only works when building a lib or example library target. π
π 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 theterm.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 fromrustc. cannot be used withhumanorshort.json-diagnostic-rendered-ansi: ensure therenderedfield of JSON messages contains embedded ANSI color codes for respectingrustc's default color scheme. cannot be used withhumanorshort.
to be continued, bestie β there's more of this page coming π
json-render-diagnosticsβ tells Cargo to NOT put rustc's diagnostics inside 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, still get emitted like normal. Cannot be used withhumanorshort. pick a lane bestie π
π Manifest Options π
These ones are all about which manifest and which dependency resolution we're rolling with. lowkey the most important section, ngl.
--manifest-pathpath β the path to theCargo.tomlfile. If you don't say anything, Cargo goes looking forCargo.tomlin the current directory, and then keeps climbing up through every parent directory until it finds one. π§--ignore-rust-versionβ just... ignore therust-versionspecification in packages. pretend you didn't see it. π--lockedβ asserts that the exact same dependencies and versions are used as when the existingCargo.lockfile was originally generated. Cargo will bail out with an error when either of these happens:- the lock file is straight up missing π«₯
- Cargo tried to change the lock file because the dependency resolution came out different
it's giving very "we are not doing surprises today". use it in environments where you want deterministic builds, like CI pipelines. β
--offlineβ stops Cargo from touching the network for literally 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 will attempt to keep going without the network if it possibly can. πΈ
β οΈ beware bestie, this can give you a different dependency resolution than online mode. Cargo will restrict itself to crates that are already downloaded locally, even if a newer version exists as indicated in the local copy of the index. See the cargo-fetch(1) command to download 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β literally equivalent to specifying both--lockedand--offlineat the same time. one flag, two flags' worth of chaos. π
β¨ Common Options β¨
+toolchainβ if Cargo was installed with rustup, and the first argument tocargostarts with+, it gets interpreted as a rustup toolchain name (things like+stableor+nightly). See the rustup documentation athttps://rust-lang.github.io/rustup/overrides.htmlfor more on how toolchain overrides work. π¦--configKEY=VALUE or PATH β overrides a Cargo configuration value. The argument should be in TOML syntax ofKEY=VALUE, or given as a path to an extra configuration file. You can pass this flag multiple times, no cap. See the command-line overrides section athttps://doc.rust-lang.org/cargo/reference/config.html#command-line-overridesfor more info. β¨-CPATH β changes the current working directory before executing any of the specified operations. This affects stuff like where cargo looks by default for the project manifest (Cargo.toml), and also the directories it searches when discovering.cargo/config.toml, for example. This option must appear before the command name, like:
cargo -C path/to/my-project build
π heads up: this option is only available on the nightly channel (https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and it requires the-Z unstable-optionsflag to enable (see #10098 athttps://github.com/rust-lang/cargo/issues/10098).
-h,--helpβ prints help information. the classic. π«Ά-Zflag β unstable (nightly-only) flags for Cargo. Runcargo -Z helpfor the details. iykyk. π₯
π Miscellaneous Options π
-jN,--jobsN β the 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 it's negative, it sets the maximum number of parallel jobs to the number of logical CPUs plus the provided value. If the stringdefaultis provided, it sets the value back to defaults. Should not be 0. π«--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. queen behavior honestly π
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 at https://doc.rust-lang.org/cargo/reference/environment-variables.html for details on environment variables that Cargo reads. β¨
π EXIT STATUS π
0β Cargo succeeded. we love to see it π101β Cargo failed to complete. it's giving tragedy π
π¦ 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 π
and that's the whole page bestie, no crumbs left π this was CARGO-RUSTC(1) straight from the Cargo manual β the official source, with its version and date up in the header β so go forth and cargo rustc responsibly. mwah π
β¨
π¦ where this page comes from
- ubuntu jammy: cargo-1.81 1.81.0+dfsg0ubuntu0-0ubuntu0.22.04.1 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.81 1.81.0+dfsg0ubuntu1-0ubuntu0.24.04.1 Β· 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