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())));

    dbg!(result);
}

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

Playground

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