macro

panic! — Signal an unrecoverable condition in the current thread

Start a panic in the current thread when execution reaches the macro. In Rust 2024 a formatted panic uses a literal template. Depending on panic strategy and context, unwinding or aborting can occur; this is not ordinary Result-based error propagation. Expression result: !. Expected result (not executed): stdout: 'Units: 4\n'; no stderr. Common mistake: Using panic! for a routine input error in a service API. Return Result instead when recovery is expected.

Formal Syntax & Structure

rust syntaxrust
panic!("message", arguments...)

Working Example

Example Snippetrust
fn units(count: i32) -> i32 {    if count < 0 { panic!("negative count: {count}"); }    count}fn main() { println!("Units: {}", units(4)); }
Rust Reference