Continuous Integration
Continuous Integration (CI) automates formatting checks, static analysis, unit and integration tests, documentation verification, and security audits on every pull request and commit. A well-structured Rust CI pipeline fails fast on any regression while caching Cargo's registry and incremental build artifacts.
Production Rust CI Pipeline Stages:
[ Stage 1: Fast Static Gates (<30s) ]
├── `cargo fmt --all -- --check`
└── `cargo deny check` / `cargo audit`
|
v
[ Stage 2: Lint & Documentation Verification ]
├── `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`
└── `RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --all-features --locked`
|
v
[ Stage 3: Multi-OS & MSRV Test Matrix ]
├── `cargo test --workspace --all-targets --all-features --locked` (Linux, macOS, Windows)
├── `cargo test --workspace --doc --all-features --locked`
└── MSRV Check (`toolchain: "1.85.0"`)
Complete GitHub Actions Workflow for a Rust Workspace
Below is a production-ready .github/workflows/ci.yml configuration incorporating --locked verification, zero-warning gates, and multi-platform matrix testing:
name: CI
on:
push:
branches: [main]
pull_request:
env:
CARGO_TERM_COLOR: always
RUSTFLAGS: "-D warnings"
RUSTDOCFLAGS: "-D warnings"
jobs:
fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- run: cargo fmt --all -- --check
clippy-and-docs:
name: Clippy & Docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- run: cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
- run: cargo doc --workspace --no-deps --all-features --locked
test:
name: Test Suite (${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- run: cargo test --workspace --all-targets --all-features --locked
- run: cargo test --workspace --doc --all-features --locked
Measuring Source-Based Code Coverage (cargo-llvm-cov)
Rust's compiler includes native LLVM source-based code coverage instrumentation (-C instrument-coverage), which tracks exact expression and branch execution counts without the inaccuracies of DWARF line-table sampling. The ecosystem tool cargo-llvm-cov (cargo install cargo-llvm-cov --locked) wraps this compiler pass into a single command:
# 1. Install the llvm-tools-preview component and cargo-llvm-cov:
rustup component add llvm-tools-preview
cargo install cargo-llvm-cov --locked
# 2. Run tests and generate an HTML coverage report in target/llvm-cov/html/:
cargo llvm-cov --all-features --workspace --html
# 3. Export an LCOV report for CI coverage badges or enforce a minimum threshold:
cargo llvm-cov --all-features --workspace --fail-under-lines 85
pub fn classify_status(code: u16) -> &'static str {
match code {
200..=299 => "success",
300..=399 => "redirect",
400..=499 => "client_error",
500..=599 => "server_error",
_ => "unknown",
}
}
fn main() {
assert_eq!(classify_status(200), "success");
assert_eq!(classify_status(404), "client_error");
assert_eq!(classify_status(503), "server_error");
println!("CI status checks passed");
}
CI status checks passed