How would this example be in older rust?

This is a very beginner question.

In the release post for 1.88, they define Let Chains which look nice:

if let Channel::Stable(v) = release_info()
    && let Semver { major, minor, .. } = v
    && major == 1
    && minor == 88
{
    println!("`let_chains` was stabilized in this version");
}

I assume without the chains this would be

if let Channel::Stable(v) = release_info(){
    if let  let Semver { major, minor, .. } = v {
        if major == 1 && minor == 88 {
            println!("`let_chains` was stabilized in this version");
      }
   }
}

Or how?

It's a curious example as it doesn't require nesting to avoid let chains.

if let Channel::Stable(Semver { major: 1, minor: 88, .. }) = release_info() {
    println!("`let_chains` was stabilized in this version");
}
2 Likes

Maybe we/you should contact the Release Team.

I think @ehuss may be appropriate.

So my code is unnecessarily nested but still semantically and syntactically correct?

Oh, well, disregarding the double let I left over in the first nested block.

Yep!

Let chains are a cool feature, even if that wasn't the best demonstration. Check out the diff in this PR[1] for some more real-world examples, where you bind some variable in the let and then call methods and/or do dynamic comparisons with those variables in the chain.

        if let Some(host_clause) = assumption.as_host_effect_clause()
            && host_clause.def_id() == goal.predicate.def_id()
            && host_clause.constness().satisfies(goal.predicate.constness)
And TBF even this could be rewritten to avoid nesting without let chain, given enough mapping and whatnot on the RHS. But I wouldn't have -- whereas with the original example, I would have written it like I showed, and probably still would.
  1. chosen at random ↩︎

1 Like

FWIW, I originally had what I thought was a stronger example, but feedback was that it was confusing:

... although that one could probably also use a single nested pattern if you really wanted.

1 Like

Yeah, it's a judgement call which is nicer for a given piece of code, and it's true that the pattern itself does nest in my version too.[1] I've definitely used multiple let something = ...; let something = ...; statements to break up awkward nesting and method chains pre 1.88, and I imagine working with let chaining will lead to analogous code, such as in your original example.


  1. Though the if [let] blocks don't, which is what the notes talk about. ↩︎