Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Defensive Programming

Assertions verify invariants that must hold for the program to be correct. Choosing the right assertion type balances safety against runtime cost.

Use debug_assert for correctness-only checks (debug-assert)

Assertions verifying invariants that should never fail in correct code belong in debug_assert!, not assert!. debug_assert! is compiled out in release builds, so the check catches bugs during development without costing anything in production.

debug_assert!(self.align.is_multiple_of(PAGE_SIZE));
debug_assert!(self.align.is_power_of_two());

See also: std::debug_assert! and Rust Reference: debug_assertions.