I have some code that looks like this:
f(a).and_then(|b| {
g(b).and_then(|c| {
h(c).map(|d| {
do_something_with(a, b, c, d)
})
})
})
Where f
, g
, and h
return Option
values. I need to use all the intermediate values (a
, b
, c
, and d
) in the do_something_with
calculation. The indentation is very deep. Is there a better way to do this? Ideally it would look something like this (which of course doesn't work):
try {
let b = f(a);
let c = g(b);
let d = h(c);
do_something_with(a, b, c, d)
} rescue NonexistentValueException {
None
}
See Question&Answers more detail:os