Panic, unwrap and expect
panic! stops the program. unwrap and expect panic on None or Err, and expect adds a message. Use them for bugs and tests, not for expected failures.
Example
let ok: Result<i32, String> = Ok(5);
println!("{}", ok.clone().unwrap());
println!("{}", ok.expect("value required"));
let n: Option<i32> = None;
println!("{}", n.unwrap_or(0));Result
5 5 0
Exercise
Safe default.
opt.(0)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.