Here's a simple example:
if let Some(x) = y {
if let Some(t) = u {
do_thing = false;
} else {
do_thing = true;
}
} else {
do_thing = true;
}
I would think that you could just have something like...
if let Some(x) = y && let Some(t) = u {
do_thing = false;
} else {
do_thing = true;
}
...but that doesn't seem to work. Is there a clean solution to this?
if let (Some(x), Some(t)) = (y, u) {
do_thing = false;
} else {
do_thing = true;
}
if you don't need values then you can write
if y.is_some() && u.is_some() { ... }
If y
and x
are just intermediary values needed for the computation of u
, then this is exactly what .and_then
is for:
if let Some(x) = y {
if let Some(t) = f(x) {
do_thing = false;
} else {
do_thing = true;
}
} else {
do_thing = true;
}
becomes:
if let Some(t) = y.and_then(f) {
do_thing = false;
} else {
do_thing = true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With