#Chaining `let` conditions

1 messages · Page 1 of 1 (latest)

outer vector
#

I know this is probably a feature that has been asked for many times, I am of course referring to the ability to chain multiple conditions in an if let, instead of making multiple nested ifs:

if let Some(a) = my_optional {
  if a > 12 {
    ...
  }
}
// would be much better if you could write it as...
if let Some(a) = my_optional 
   && a > 12 
{
  ...
}

I have seen RFCs for this a while ago, but it's fractured into many of them and I haven't been able to find what the exact status of this feature is at the moment.

#

and, to be clear, I mean chaining conditions that actually use the first variable you destructure

#

if let and while let are wonderful features, but they break apart easily as soon as you want to add any other kind of condition onto them...

shell drum
outer vector
#

ooh

cobalt sleet
#

https://caniuse.rs is really nice for checking the status of upcoming features and stabilisation

outer vector
#

ooh that's a handy site

velvet surge
#

For this use in particular,

#

?eval ```rust
let my_optional = Some(15);
if let Some(a @ 13..) = my_optional {
println!("{a}");
}

tawny harborBOT
#
15
()
velvet surge
#

It puts whatever matches the pattern after the @ in the variable before the @.

deft atlas
#

Is it only for range syntax?

velvet surge
#

It works for any pattern.