cargo-rustc
Invoke rustc with Cargo's configuration
TLDR
Build the package and pass options to rustc
Build artifacts in release mode, with optimizations
Compile with architecture-specific optimizations for the current CPU
Compile with speed optimizations
Compile with [s]ize optimizations (z also turns off loop vectorization)
Check if your package uses unsafe code
Build a specific package
Build only the specified binary
SYNOPSIS
cargo rustc [OPTIONS] [--] [rustcflags…]
PARAMETERS
--lib
Build the crate's library target
--bin NAME
Build the specified binary
--bins
Build all binaries
--example NAME
Build the specified example
--examples
Build all examples
--bench NAME
Build the specified benchmark
--release
Build artifacts in release mode with optimizations
--profile PROFILE
Build with the given profile (debug/release)
--target TRIPLE
Target triple to build for
--target-dir DIR
Directory for target artifacts
--package SPEC…
Package(s) to build (see cargo pkgid)
--all
Build all workspace packages
--workspace
Build workspace members and dependencies
--jobs N
Number of parallel jobs
--keep-going
Build as many crates as possible despite errors
-v, --verbose
Use verbose output
--quiet
No output printed to stdout
--color WHEN
Control colored output (auto/always/never)
--manifest-path PATH
Path to Cargo.toml
-Z FLAG…
Unstable Cargo flags
--
Separator; following args passed to rustc
DESCRIPTION
cargo rustc compiles a Rust crate using the rustc compiler directly, while leveraging Cargo's dependency resolution, metadata injection, and build configuration. This subcommand is designed for scenarios where users need to pass custom or advanced flags to rustc that Cargo's higher-level commands like cargo build do not expose.
Key use cases include enabling nightly features with -Z flags, custom codegen options via -C, conditional compilation with --cfg, or experimenting with compiler internals. Cargo first builds dependencies and generates rlibs, then invokes rustc on the target crate, injecting necessary metadata for proper linking and incremental compilation support.
Unlike standard Cargo commands, cargo rustc forwards all arguments after -- verbatim to rustc, offering low-level control. It supports most cargo build options for package, target, and profile selection but skips --test. This makes it ideal for plugin development, custom linking, or optimization tweaking in complex projects.
Incremental builds work when possible, but custom rustc flags often invalidate caches, triggering recompiles. Widely used in Rust ecosystem for advanced tooling and CI setups requiring precise compiler control.
CAVEATS
Does not support --test; custom rustc flags after -- often invalidate incremental caches, causing rebuilds. Requires valid Cargo workspace.
EXAMPLES
Release build with custom opt:
cargo rustc --release -- -C opt-level=z
Nightly feature:
cargo rustc -- -Z unstable-options --cfg feature="nightly"
Specific target:
cargo rustc --target wasm32-unknown-unknown -- --emit wasm
RUSTC FLAGS
Args after -- go directly to rustc: use -C for codegen (e.g., -C lto), -Z for unstable (nightly-only), --crate-type for outputs.
HISTORY
Introduced in Cargo 0.12.0 (May 2015), cargo rustc provided direct rustc access amid growing demand for compiler experimentation as Rust stabilized post-1.0. Evolved with Cargo's unification of build options in 0.20+ (2017), aligning closely with cargo build. Remains key for nightly Rust workflows.


