macro

assert! — Enforce a runtime invariant

Require a boolean condition to hold. A false condition panics; a successful assertion has no output. Assertions remain enabled in release builds. For ordinary invalid external input, prefer a recoverable Result or other explicit error handling. Expression result: (). Expected result (not executed): stdout: 'Remaining: 3\n'; no stderr. Common mistake: Using assert! as the sole validation for expected user mistakes when the caller should be able to recover.

Formal Syntax & Structure

rust syntaxrust
assert!(condition) or assert!(condition, "message", arguments...)

Working Example

Example Snippetrust
fn main() {    let capacity = 12;    let occupied = 9;    assert!(occupied <= capacity, "capacity exceeded");    println!("Remaining: {}", capacity - occupied);}
Rust Reference