cargo build --release: Optimized Builds in CI
cargo build --release compiles the package with optimizations enabled and writes artifacts to target/release instead of target/debug.
Debug builds are the default; --release turns on opt-level 3 and is what you ship. In CI you also want --locked so the build fails rather than silently editing Cargo.lock.
What it does
cargo build --release compiles the current package and its dependencies with the release profile (optimizations on, debug assertions off) and places binaries and libraries under target/release. Without --release it uses the dev profile under target/debug.
Common usage
cargo build --release
cargo build --release --locked # fail if Cargo.lock would change
cargo build --release --target x86_64-unknown-linux-musl
cargo build --release --bin myapp -p mycrateFlags
| Flag | What it does |
|---|---|
| --release / -r | Build with the release profile (optimized) |
| --locked | Require Cargo.lock to be up to date; error instead of updating it |
| --frozen | Like --locked plus --offline (no network, no lock changes) |
| --target <triple> | Cross-compile for the given target triple |
| --bin <name> / --lib | Build a specific binary or just the library |
| -p <crate> | Build only the named workspace package |
In CI
Cache ~/.cargo/registry and ~/.cargo/git plus the target/ directory keyed on Cargo.lock to skip re-downloading and re-compiling dependencies. Always pass --locked so a stale Cargo.lock fails the job instead of being rewritten. For --target, the toolchain must have it installed via rustup target add <triple>.
Common errors in CI
"error: the lock file ... needs to be updated but --locked was passed to prevent this" means Cargo.lock is out of sync; run cargo update locally and commit it. "error[E0463]: can't find crate for core" with --target means the std for that triple is missing; add rustup target add <triple>. "linking with cc failed: ... -lssl" means a system library is missing in the runner image.