Skip to content
Latchkey

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 app

Common 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 cc failed - missing system linker or C dependency

Key takeaways

  • -O turns on optimizations; pair with --edition to 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →