I use struct variants usually when some variants have more fields and no logical ordering, like for state machines.
But there I’d prefer to use
let (foo, bar) = match {
State::A { foo, bar } => (foo, bar),
State::B { .. } => return Err(..),
};
do_something(foo, bar);
kind of construct, because it’s easier to adjust when one has to react to multiple variants:
let (foo, bar) = match {
State::A { foo, bar } => (foo, bar),
State::B { .. } => return Err(..),
State::X { foo } => (foo, None), // new variant
};
do_something(foo, bar);
But that doesn’t mean there aren’t any situations where it could be useful.
One syntax I think I haven’t seen yet is
let if Some(x) = expr else {
log!();
bail!();
};
This one
- Doesn’t need a new keyword.
- Still has
let as current-scope-binding keyword.
- Has an early
if implying some form of condition.
- Has some symmetry with
if let (if let <cond> <scope> <else> into let if <cond> <else>; <scope> because the initial let means “current scope”).
- Makes the
else more expected at the end due to the if directly after the let.