CARGO-RUSTC(1)
π 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.
-pspec,--packagespec β The package to build. For the whole SPEC format situation, seecargo-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,--testand--benchalso 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 thetest = truemanifest 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 thetestflag 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 thebench = truemanifest 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 thebenchflag 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>.
-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, 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 π₯
--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.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 thereleaseprofile. See also the--profileoption for choosing a specific profile by name. The classic "ship it" button.--profilename β Build with the given profile.
The rustc subcommand treats these 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. (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;--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. 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-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 thecrate-typefield<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-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. 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 theterm.verboseconfig 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 theterm.quietconfig value<https://doc.rust-lang.org/cargo/reference/config.html>. Silence, gorgeous. The quiet luxury aesthetic π€«--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.color config value <https://doc.rust-lang.org/cargo/reference/config.html>. Rainbow or no rainbow, your call π
--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 tostdout. 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.
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 withhumanorshort. No mixing, periodt.
π Manifest Options π
--manifest-pathpath β where theCargo.tomlfile lives. By default Cargo goes hunting forCargo.tomlin the current directory, or any parent directory above it. π¦--ignore-rust-versionβ straight up ignore therust-versionspecification in packages. Not me pretending that field doesn't exist π--lockedβ asserts that the exact same dependencies and versions get used as when the existingCargo.lockfile 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--lockedand--offlineat once. Two for one, iconic. π--lockfile-pathPATH β moves the path of the lockfile from the default (<workspace_root>/Cargo.lock) to PATH. PATH must end withCargo.lock(like--lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that handing over--lockfile-pathwill 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-optionsflag 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 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. π¦--configKEY=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. β¨-CPATH β 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-optionsflag to enable (see #10098 βhttps://github.com/rust-lang/cargo/issues/10098).
-h,--helpβ prints help information. The classic "help me help me" move π«Ά-Zflag β unstable (nightly-only) flags to Cargo. Runcargo -Z helpfor 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. 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 π
- 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)
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
- ubuntu questing: cargo-1.88 1.88.0+dfsg0ubuntu1-0ubuntu1
- ubuntu questing: cargo-1.88 1.88.0+dfsg0ubuntu1-0ubuntu2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms