Skip to content
Latchkey

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 dependencies
  • cargo 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 --locked

Common 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 X found - wrong name or missing registry
  • error: failed to run custom build command for openssl-sys - missing system libs

Key takeaways

  • Use --locked in CI so builds are reproducible and lockfile drift fails fast.
  • cargo test also runs doc tests; --all-features widens coverage.
  • Cache the target dir and ~/.cargo to cut compile time on managed runners.

Related guides

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