CARGO-RUSTC(1)
cargo-rustc(1) โ General Commands Manual
โจ NAME โจ
cargo-rustc โ Compile the current package, and pass extra options to the compiler
๐ฆ SYNOPSIS ๐ฆ
cargo rustc [options] [-- args]
๐ DESCRIPTION ๐
Okay bestie, here's the vibe: the specified target for the current package (or the package you picked with -p if you provided it) gets compiled along with all of its dependencies. The specified args will all be passed to the final compiler invocation, not to any of the dependencies. And just so you know, the compiler will still unconditionally receive arguments such as -L, --extern, and --crate-type, and the specified args will simply be added to the compiler invocation. No cap.
See <https://doc.rust-lang.org/rustc/index.html> for documentation on rustc flags.
This command requires that only one target is being compiled when additional arguments are provided. If more than one target is available for the current package, the filters of --lib, --bin, etc, must be used to select which target is compiled.
To pass flags to all compiler processes spawned by Cargo, 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 ๐ฆ
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.
-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.
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 <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates> is set when the integration test is built and run so that it can use the env macro <https://doc.rust-lang.org/std/macro.env.html> or the var function <https://doc.rust-lang.org/std/env/fn.var.html> to locate the executable.
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. Periodt.
--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 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 thetestflag 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 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 thebenchflag 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 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 <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 specified target architecture. Flag may be specified multiple times. The default is the host architecture. The general format of the triple is<arch><sub>-<vendor>-<sys>-<abi>.
Possible values:
- Any supported target in
rustc --print target-list. "host-tuple", which will internally be substituted by the hostโs target. This can be particularly useful if youโre cross-compiling some crates, and donโt want to specify your hostโs machine as a target (for instance, an xtask in a shared project that may be worked on by many hosts).- A path to a custom target specification. See Custom Target Lookup Path
<https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path>for more information.
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 <https://doc.rust-lang.org/cargo/reference/build-cache.html> documentation for more details.
-r,--releaseโ Build optimized artifacts with the release profile. See also the--profileoption for choosing a specific profile by name.--profilename โ Build with the given profile.
The rustc subcommand 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.
--timingsโ Output information how long each compilation takes, and track concurrency information over time.
A file cargo-timing.html will be written to the target/cargo-timings directory at the end of the build. An additional report with a timestamp in its filename is also written if you want to look at a previous run. These reports are suitable for human consumption only, and do not provide machine-readable timing data.
--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 ascrate-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-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-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.color config 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โ okay so this tells Cargo to not include rustc diagnostics in the JSON messages printed. Instead, Cargo itself should render the JSON diagnostics coming from rustc. Cargoโs own JSON diagnostics and others coming from rustc are still emitted, no cap. Cannot be used withhumanorshort. ๐
โจ Manifest Options โจ
--manifest-pathpath โ path to theCargo.tomlfile. By default, Cargo searches for theCargo.tomlfile in the current directory or any parent directory. ๐๏ธ--ignore-rust-versionโ ignorerust-versionspecification in packages. Like, pretend itโs not even there. ๐--lockedโ asserts that the exact same dependencies and versions are used as when the existingCargo.lockfile was originally generated. Cargo will exit with an error when either of the following 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. periodt.
--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 that 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 thecargo-fetch(1)command to download dependencies before going offline. May also be specified with thenet.offlineconfig valuehttps://doc.rust-lang.org/cargo/reference/config.html. ๐--frozenโ equivalent to specifying both--lockedand--offline. Two for one, slay. โ๏ธ
โจ 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+stableor+nightly). See the rustup documentationhttps://rust-lang.github.io/rustup/overrides.htmlfor more information about how toolchain overrides work. ๐ฆ--configKEY=VALUE or PATH โ overrides a Cargo configuration value. The argument should be in TOML syntax ofKEY=VALUE, or provided as a path to an extra configuration file. This flag may be specified multiple times. See the command-line overrides sectionhttps://doc.rust-lang.org/cargo/reference/config.html#command-line-overridesfor more information. ๐ง-CPATH โ 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 examplecargo -C path/to/my-project build. ๐งญ This option is only available on the nightly channelhttps://doc.rust-lang.org/book/appendix-07-nightly-rust.htmland requires the-Z unstable-optionsflag to enable (see #10098https://github.com/rust-lang/cargo/issues/10098). ๐-h,--helpโ prints help information. The classic. ๐-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 valuehttps://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 stringdefaultis provided, it sets the value back to defaults. Should not be0. โก--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 dependenciesfailsandworks, one of which fails to build,cargo rustc -j1may or may not build the one that succeeds (depending on which one of the two builds Cargo picked to run first), whereascargo rustc -j1 --keep-goingwould 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. Seecargo-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. ๐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 which prints the size of every type:
cargo rustc --lib -- -Z print-type-sizes
- Override
crate-typefield inCargo.tomlwith command-line option:
cargo rustc --lib --crate-type lib,cdylib
๐ SEE ALSO ๐
cargo(1), cargo-build(1), rustc(1)
and thatโs the tea, bestie ๐ source/version is CARGO-RUSTC(1) and the date is whenever youโre reading this โ xoxo, stay slay ๐
โจ
๐ฆ where this page comes from
- ubuntu stonking: cargo-1.95 1.95.0+dfsg-0ubuntu1 ยท 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