Result

Result is a richer version of the Option type that
describes possible error instead of possible absence.

That is, Result<T, E> could have one of two outcomes:

  • Ok<T>: An element T was found
  • Err<E>: An error was found with element E

By convention, the expected outcome is Ok while the unexpected outcome is Err.

Like Option, Result has many methods associated with it. unwrap(), for
example, either yields the element T or panics. For case handling,
there are many combinators between Result and Option that overlap.

In working with Rust, you will likely encounter methods that return the
Result type, such as the parse() method. It might not always
be possible to parse a string into the other type, so parse() returns a
Result indicating possible failure.

Let’s see what happens when we successfully and unsuccessfully parse() a string:

  1. fn multiply(first_number_str: &str, second_number_str: &str) -> i32 {
  2. // Let's try using `unwrap()` to get the number out. Will it bite us?
  3. let first_number = first_number_str.parse::<i32>().unwrap();
  4. let second_number = second_number_str.parse::<i32>().unwrap();
  5. first_number * second_number
  6. }
  7. fn main() {
  8. let twenty = multiply("10", "2");
  9. println!("double is {}", twenty);
  10. let tt = multiply("t", "2");
  11. println!("double is {}", tt);
  12. }

In the unsuccessful case, parse() leaves us with an error for unwrap()
to panic on. Additionally, the panic exits our program and provides an
unpleasant error message.

To improve the quality of our error message, we should be more specific
about the return type and consider explicitly handling the error.