Tour durch Rust Inhaltsverzeichnis

[Noch nicht übersetzt] Ugly Option/Result Handling

Working with Option/Result can be tedious when you are just trying to write some quick code. Both Option and Result have a function called unwrap that can be useful for getting a value in a quick and dirty manner. unwrap will:

  1. Get the value inside Option/Result
  2. If the enum is of type None/Err, panic!

These two pieces of code are equivalent:

my_option.unwrap()
match my_option {
    Some(v) => v,
    None => panic!("some error message generated by Rust!"),
}

Similarly:

my_result.unwrap()
match my_result {
    Ok(v) => v,
    Err(e) => panic!("some error message generated by Rust!"),
}

Be a good rustacean and properly use match when you can!