Part 6 · 1 chapters · ~8 min

Tooling and Testing

cargo for building, testing, benchmarking and documentation, workspaces and features, rustfmt and clippy, unit tests beside code and integration tests in tests/, doc tests, proptest and cargo-fuzz, criterion benchmarks, cargo-audit and cargo-deny, and keeping compile times reasonable.

10

cargo does it all

code
cargo build --release      cargo test      cargo bench      cargo doc --open
cargo fmt                  cargo clippy -- -D warnings      cargo audit      cargo deny check

// unit tests live beside the code
#[cfg(test)]
mod tests {
    use super::*;
    #[test] fn minimum_fee_applies() { assert_eq!(fee(100_000).unwrap(), 1_000); }
    #[test] fn zero_is_rejected() { assert!(fee(0).is_err()); }
}
// property tests (proptest): postings always balance
proptest! { #[test] fn balanced(ops in vec(any_op(), 1..500)) { let l = apply(ops); prop_assert_eq!(l.sum(), 0); } }

/// Doc comments with examples are compiled and run as tests:
/// ```
/// assert_eq!(ledger::fee(5_000_000).unwrap(), 25_000);
/// ```
compile-time levereffect
split into workspace cratesparallel and incremental builds
fewer heavy generic dependencies, feature flags off by defaultless code to compile
cargo check while editingtype-checks without code generation
a faster linker (mold, lld) and sccache in CIshorter link and rebuild times