cargo Command: Build & Test Rust in CI
cargo builds, tests, and packages Rust crates and manages their dependencies.
cargo is the standard entry point for Rust CI: it resolves dependencies from crates.io, compiles via rustc, and runs tests. The build/test subcommands plus a handful of flags cover almost every pipeline step.
Common flags
cargo build- compile the package and its dependenciescargo test- build and run unit, integration, and doc tests--release- optimized build (slower compile, faster binary)--locked- fail if Cargo.lock is out of date (deterministic CI)--all-features/--no-default-features- control feature flags--workspace- operate on all members of the workspace--target-dir DIR- relocate build output (handy for caching)
Example in CI
A reproducible release build and test pass with locked dependencies:
shell
cargo build --release --locked
cargo test --workspace --all-features --lockedCommon errors in CI
- error: the lock file Cargo.lock needs to be updated but --locked was passed
- error: failed to select a version for
X- incompatible version constraints - error: no matching package named
Xfound - wrong name or missing registry - error: failed to run custom build command for
openssl-sys- missing system libs
Key takeaways
- Use
--lockedin CI so builds are reproducible and lockfile drift fails fast. cargo testalso runs doc tests;--all-featureswidens coverage.- Cache the target dir and
~/.cargoto cut compile time on managed runners.
Related guides
rustc Command: Compile Rust in CIrustc is the Rust compiler. Reference for -O, --edition, --crate-type, -C opt-level, --emit, and the E04xx er…
go build Command: Compile Go in CIgo build compiles Go packages. Reference for -o, -ldflags, -tags, -race, GOOS/GOARCH cross-compilation, and t…
make Command: Run Builds in CImake runs builds from a Makefile. Reference for -j, -C, -f, -B, -k, VAR=value overrides, and the Makefile err…