macro
unreachable! — Report an impossible branch reached at runtime
Mark a branch that should be impossible under the program's invariant but that the type checker cannot rule out. Reaching it panics safely. This is different from unsafe unreachable_unchecked, whose incorrect use can cause undefined behavior. Expression result: !. Expected result (not executed): stdout: 'odd\n'; no stderr. Common mistake: Using unreachable! to suppress legitimate cases in an incomplete match.
Formal Syntax & Structure
rust syntaxrust
unreachable!() or unreachable!("message", arguments...)Working Example
Example Snippetrust
fn parity(n: u32) -> &'static str { match n % 2 { 0 => "even", 1 => "odd", _ => unreachable!("remainder must be 0 or 1"), }}fn main() { println!("{}", parity(7)); }