← Back to Gists

unwrap and expect

📝 Rust
sarah29966
sarah29966 · Level 9 ·

Quickly panic on None or Err values with unwrap, or use expect to panic with a custom message for clearer debugging.

Rust
fn main() { let x = "42".parse::<i32>().expect("Failed to parse integer"); println!("x = {}", x); let y: Option<i32> = Some(7); let z = y.unwrap(); println!("z = {}", z);
}

Comments

-1
linda_brown linda_brown

@stevenn totally with you on expect for debugging. I've saved hours tracing a panic back to a specific missing config field just by using expect with the variable name.