You could try the if_chain crate to see if it's worth for your usecase, otherwise you'll have to be satisfied with the workaround, since you can't use unstable features on stable.
Honestly the first seems more natural to me. In the first example an expression must match a pattern and another expression must match another pattern, while in the second example you introduce a tuple and mix the two patterns just to get the same result. The fact that the first syntax was added to the language (despite still being unstable) is a kind of proof that people want it (though it should be noted that it is also more powerful than what was shown here, so there are other reasons other than being more natural).
If you want to track the feature, this looks like its tracking issue. Tracking issues won't always tell you when stabilization will happen, nor should any mention of future stabilizations be read as promises, but it'll let you get an idea about what progress is being made toward stabilization.
As a C user coming to Rust I would have guessed that the fact that the second syntax was added to the language is a kin of proof that people want it. So why the need for the first ?
if let Some(receiver) = receiver && let Some(property) = receiver.property {
…
}
with tuples trick.
P.S. It's funny how everyone ignore the question in the title: why do we need Rust 2024 for that to work . It's because of subtle change in Rust 2024. I wonder why editions guide seems to be silent about that change, though.
Well… it combines two unrelated things into one, then pulls it apart again… sure, optimizer is clever enough to understand what goes on here… and with Rust 2015/18/21 that's the only choice… but why?
In spite of subtle issues related to the implementation if let chaining is extremely natural… so natural that topicstarter started his message not with question “when would that be available”, but more of “why the heck this still doesn't work”…
Different people have different opinions, ultimately, but I wouldn't prefer tuple hack in a language where it's not needed.
The more general solution is to nest if statements.
if let Some(mut receiver) = receiver {
if let Some(mut sender) = sender {
...
}
}
I'm sure you don't want that, but note that your solution only works in certain cases. What I run into more frequently is the need to:
Destructure with if let
Check a condition using the destructured value.
Then do something with the destructured value.
For example, I would love to use let chaining like this:
if let Some(val) = &opt_val && cond(val) {
val.xxx();
}
But for now I have to use nested if statements:
if let Some(val) = &opt_val {
if cond(val) {
val.xxx();
}
}
I don't use Option::is_some_and since I would have to destructure afterwards to get the value. The only Option combinators that work are iter/filter/for_each:
I think that's less readable than the if statements. You can use a for "loop" also. But that's also not as readable (and very weird) IMO:
for val in opt_val.iter().filter(|val| cond(val)) {
val.xxx();
}
Perhaps some people prefer the combinators.
Anyway, I don't consider nested if statements a "workaround". I can live without chained lets and I can understand why other things are more important to change in the language. Nesting is fine and I try not to let it bother me.
I totally agree the chain of if let is more pleasant to use in this situation, I just want to point out you can do it without chaining, though not with tuples, and is very verbose in syntax: