Where is `inspect` on Option?


#1

Hello,

I want to apply a function that returns () on an Option and I miss an inspect() method. There are many workarouds like :

  • using match but it’s not “beautiful”
  • using iter().inspect() but it’s a little verbose
  • using map but it’s generally considered poor style in functional programming

What do you use ?


#2

I think I most often use if let Some(x) = opt { ... }. This is basically just sugar for a match, but maybe more “beautiful” since you don’t have to say anything about None.


#3

I do something only if the function returning Option gave a Some value:

if let Some(x) = some_func(y, z) { 
  x.foo();
}

I want a value and have a default value in mind:

let x = some_func(y, z).unwrap_or(AwesomeValues::Default);

I want a value or I give up and want to leave the function:

let x = try!(some_func(y, z).ok_or("We could not continue because some_func could not return a valid guffin."));