Converting between Option and Result

We also have methods where one wrapper type can be converted into another, depending on how you want to compose those values with your APIs. They become really handy in situations where we are interacting with third-party crates, where we have a value as an Option, but the crate's method we are using accepts a Result as a type, as follows:

  • ok_or: This method converts an Option value to a Result value, by taking in an error value as a second parameter. A similar variant to this is the ok_or_else method, which should be preferred over this, as it computes the value lazily by taking in a closure.
  • ok: This method converts a Result into an Option consuming self, and discards the Err value.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset