Multiple error types

The previous examples have always been very convenient; Results interact
with other Results and Options interact with other Options.

Sometimes an Option needs to interact with a Result, or a
Result<T, Error1> needs to interact with a Result<T, Error2>. In those
cases, we want to manage our different error types in a way that makes them
composable and easy to interact with.

In the following code, two instances of unwrap generate different error
types. Vec::first returns an Option, while parse::<i32> returns a
Result<i32, ParseIntError>:

  1. fn double_first(vec: Vec<&str>) -> i32 {
  2. let first = vec.first().unwrap(); // Generate error 1
  3. 2 * first.parse::<i32>().unwrap() // Generate error 2
  4. }
  5. fn main() {
  6. let numbers = vec!["42", "93", "18"];
  7. let empty = vec![];
  8. let strings = vec!["tofu", "93", "18"];
  9. println!("The first doubled is {}", double_first(numbers));
  10. println!("The first doubled is {}", double_first(empty));
  11. // Error 1: the input vector is empty
  12. println!("The first doubled is {}", double_first(strings));
  13. // Error 2: the element doesn't parse to a number
  14. }

Over the next sections, we’ll see several strategies for handling these kind of problems.