Get first non-None option without indentation hell

I would like the first option that is not None. Only when none of the Options has anything, then I want None. Here is some code that accomplishes that:

fn main() {
    let result = a().or_else(|| b().or_else(|| c().or_else(|| d())));


fn a() -> Option<u32> { None }
fn b() -> Option<u32> { None }
fn c() -> Option<u32> { None }
fn d() -> Option<u32> { None }


But this closure-in-closure-in-closure style is difficult to read. Can I rewrite it to have no nested closures?

1 Like

let result = a().or_else(b).or_else(c).or_else(d);

1 Like

:grin: :man_facepalming:

1 Like

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.