Inverse of Option<T>?

I have a piece of code that looks like:

if let Some(x) = side_effect_1() {
  return Some(x);
}

if let Some(x) = side_effect2() {
  return Some(x);
}

if let Some(x) = side_effect_3() {
  return Some(x);
}

return None

This is almost like the inverse of '?'. Is there a better way to refactor this?

One way is the following:

return side_effect_1()
    .or_else(side_effect_2)
    .or_else(side_effect_3);
4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.