Rust Engineering Practices
Quick Reference Card
Cheat Sheet: Commands at a Glance
# ─── Build Scripts ───cargo build # Compiles build.rs first, then cratecargo build -vv # Verbose — shows build.rs output# ─── Cross-Compilation ───rustup target add x86_64-unknown-linux-muslcargo build --release --target x86_64-unknown-linux-muslcargo zigbuild --release --target x86_64-unknown-linux-gnu.2.17cross build --release --target aarch64-unknown-linux-gnu# ─── Benchmarking ───cargo bench # Run all benchmarkscargo bench -- parse # Run benchmarks matching "parse"cargo flamegraph -- --args # Generate flamegraph from binaryperf record -g ./target/release/bin # Record perf dataperf report # View perf data interactively# ─── Coverage ───cargo llvm-cov --html # HTML reportcargo llvm-cov --lcov --output-path lcov.infocargo llvm-cov --workspace --fail-under-lines 80cargo tarpaulin --out Html # Alternative tool# ─── Safety Verification ───cargo +nightly miri test # Run tests under MiriMIRIFLAGS="-Zmiri-disable-isolation" cargo +nightly miri testvalgrind --leak-check=full ./target/debug/binaryRUSTFLAGS="-Zsanitizer=address" cargo +nightly test -Zbuild-std --target x86_64-unknown-linux-gnu# ─── Audit & Supply Chain ───cargo audit # Known vulnerability scancargo audit --deny warnings # Fail CI on any advisorycargo deny check # License + advisory + ban + source checkscargo deny list # List all licenses in dep treecargo vet # Supply chain trust verificationcargo outdated --workspace # Find outdated dependenciescargo semver-checks # Detect breaking API changescargo geiger # Count unsafe in dependency tree# ─── Binary Optimization ───cargo bloat --release --crates # Size contribution per cratecargo bloat --release -n 20 # 20 largest functionscargo +nightly udeps --workspace # Find unused dependenciescargo machete # Fast unused dep detectioncargo expand --lib module::name # See macro expansionscargo msrv find # Discover minimum Rust versioncargo clippy --fix --workspace --allow-dirty # Auto-fix lint warnings# ─── Compile-Time Optimization ───export RUSTC_WRAPPER=sccache # Shared compilation cachesccache --show-stats # Cache hit statisticscargo nextest run # Faster test runnercargo nextest run --retries 2 # Retry flaky tests# ─── Platform Engineering ───cargo check --target thumbv7em-none-eabihf # Verify no_std buildscargo build --target x86_64-pc-windows-gnu # Cross-compile to Windowscargo xwin build --target x86_64-pc-windows-msvc # MSVC ABI cross-compilecfg!(target_os = "linux") # Compile-time cfg (evaluates to bool)# ─── Release ───cargo release patch --dry-run # Preview releasecargo release patch --execute # Bump, commit, tag, publishcargo dist plan # Preview distribution artifactsDecision Table: Which Tool When
| Goal | Tool | When to Use |
|---|---|---|
| Embed git hash / build info | build.rs | Binary needs traceability |
| Compile C code with Rust | cc crate in build.rs | FFI to small C libraries |
| Generate code from schemas | prost-build / tonic-build | Protobuf, gRPC, FlatBuffers |
| Link system library | pkg-config in build.rs | OpenSSL, libpci, systemd |
| Static Linux binary | --target x86_64-unknown-linux-musl | Container/cloud deployment |
| Target old glibc | cargo-zigbuild | RHEL 7, CentOS 7 compatibility |
| ARM server binary | cross or cargo-zigbuild | Graviton/Ampere deployment |
| Statistical benchmarks | Criterion.rs | Performance regression detection |
| Quick perf check | Divan | Development-time profiling |
| Find hot spots | cargo flamegraph / perf | After benchmark identifies slow code |
| Line/branch coverage | cargo-llvm-cov | CI coverage gates, gap analysis |
| Quick coverage check | cargo-tarpaulin | Local development |
| Rust UB detection | Miri | Pure-Rust unsafe code |
| C FFI memory safety | Valgrind memcheck | Mixed Rust/C codebases |
| Data race detection | TSan or Miri | Concurrent unsafe code |
| Buffer overflow detection | ASan | unsafe pointer arithmetic |
| Leak detection | Valgrind or LSan | Long-running services |
| Local CI equivalent | cargo-make | Developer workflow automation |
| Pre-commit checks | cargo-husky or git hooks | Catch issues before push |
| Automated releases | cargo-release + cargo-dist | Version management + distribution |
| Dependency auditing | cargo-audit / cargo-deny | Supply chain security |
| License compliance | cargo-deny (licenses) | Commercial / enterprise projects |
| Supply chain trust | cargo-vet | High-security environments |
| Find outdated deps | cargo-outdated | Scheduled maintenance |
| Detect breaking changes | cargo-semver-checks | Library crate publishing |
| Dependency tree analysis | cargo tree --duplicates | Dedup and trim dep graph |
| Binary size analysis | cargo-bloat | Size-constrained deployments |
| Find unused deps | cargo-udeps / cargo-machete | Trim compile time and size |
| LTO tuning | lto = true or "thin" | Release binary optimization |
| Size-optimized binary | opt-level = "z" + strip = true | Embedded / WASM / containers |
| Unsafe usage audit | cargo-geiger | Security policy enforcement |
| Macro debugging | cargo-expand | Derive / macro_rules debugging |
| Faster linking | mold linker | Developer inner loop |
| Compilation cache | sccache | CI and local build speed |
| Faster tests | cargo-nextest | CI and local test speed |
| MSRV compliance | cargo-msrv | Library publishing |
no_std library | #![no_std] + default-features = false | Embedded, UEFI, WASM |
| Windows cross-compile | cargo-xwin / MinGW | Linux → Windows builds |
| Platform abstraction | #[cfg] + trait pattern | Multi-OS codebases |
| Windows API calls | windows-sys / windows crate | Native Windows functionality |
| End-to-end timing | hyperfine | Whole-binary benchmarks, before/after comparison |
| Property-based testing | proptest | Edge case discovery, parser robustness |
| Snapshot testing | insta | Large structured output verification |
| Coverage-guided fuzzing | cargo-fuzz | Crash discovery in parsers |
| Concurrency model checking | loom | Lock-free data structures, atomic ordering |
| Feature combination testing | cargo-hack | Crates with multiple #[cfg] features |
| Fast UB checks (near-native) | cargo-careful | CI safety gate, lighter than Miri |
| Auto-rebuild on save | cargo-watch | Developer inner loop, tight feedback |
| Workspace documentation | cargo doc + rustdoc | API discovery, onboarding, doc-link CI |
| Reproducible builds | --locked + SOURCE_DATE_EPOCH | Release integrity verification |
| CI cache tuning | Swatinem/rust-cache@v2 | Build time reduction (cold → cached) |
| Workspace lint policy | [workspace.lints] in Cargo.toml | Consistent Clippy/compiler lints across all crates |
| Auto-fix lint warnings | cargo clippy --fix | Automated cleanup of trivial issues |
Further Reading
Generated as a companion reference — a companion to Rust Patterns and Type-Driven Correctness.
Version 1.3 — Added cargo-hack, cargo-careful, cargo-watch, cargo doc, reproducible builds, CI caching strategies, capstone exercise, and chapter dependency diagram for completeness.