expression

Distinguish undefined behavior from recoverable errors

Undefined behavior is not a predictable error value or panic. Code that violates memory validity or aliasing requirements is incorrect even if a particular build appears to work. Use checked interfaces when invalid input is an ordinary possibility. This fixture only demonstrates defined failure, never triggers undefined behavior. Expected stdout (not executed): two defined failures Common mistake: Testing an invalid pointer dereference and treating a crash or plausible output as proof of correctness.

Formal Syntax & Structure

rust syntaxrust
checked_add / Result / Option

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    let n = u8::MAX;    assert_eq!(n.checked_add(1), None);    let absent: Option<&i32> = [].get(0);    assert!(absent.is_none());    println!("two defined failures");}
Rust Reference