rustc Command: Compile Rust in CI
rustc compiles Rust source files into binaries, libraries, or other artifacts.
rustc is the Rust compiler that Cargo invokes under the hood. You rarely call it directly in app builds, but it shows up in CI for single-file tools, doc tests, and custom build scripts. Knowing its flags helps decode the errors Cargo surfaces.
Common flags
-O- enable optimizations (equivalent to-C opt-level=2)--edition 2021- set the Rust edition (2015, 2018, 2021, 2024)-o NAME- set the output file name--crate-type bin|lib|cdylib- choose what to emit-C opt-level=3/-C debuginfo=0- fine-grained codegen options--emit metadata|llvm-ir|asm- emit intermediate artifacts--target x86_64-unknown-linux-musl- cross-compile target triple-W warnings/-D warnings- treat warnings as errors with-D
Example in CI
Compile a standalone helper binary with optimizations during a CI job:
shell
rustc -O --edition 2021 -D warnings src/main.rs -o appCommon errors in CI
- error[E0433]: failed to resolve: use of undeclared crate or module - missing
use/dependency - error[E0432]: unresolved import - the path does not exist in scope
- error[E0502]: cannot borrow as mutable because it is also borrowed as immutable
- error: edition 2021 is unstable - toolchain too old; update via rustup
- error: linking with
ccfailed - missing system linker or C dependency
Key takeaways
-Oturns on optimizations; pair with--editionto match your codebase.- Cargo calls rustc for you in real projects; direct use is for one-off binaries.
- Most CI failures are E04xx resolution/borrow errors or a missing linker.
Related guides
cargo Command: Build & Test Rust in CIcargo is the Rust build tool and package manager. Reference for build, test, --release, --locked, --all-featu…
go build Command: Compile Go in CIgo build compiles Go packages. Reference for -o, -ldflags, -tags, -race, GOOS/GOARCH cross-compilation, and t…
clang Command: Compile C/C++ in CIclang is the LLVM C/C++ compiler. Reference for -c, -o, -I, -std, -fsanitize, -Weverything, and the diagnosti…