panic, unwrap and expect
panic! stops the program. unwrap and expect panic on None or Err. Prefer handling the case.
Example
fn main() {
let r: Result<i32, String> = Err("bad".into());
println!("{}", r.clone().unwrap_or(0));
let o: Option<i32> = None;
println!("{}", o.map_or("none".to_string(), |v| v.to_string()));
}Result
0 none
Exercise
Fallback value.
r.unwrap(0)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.