Multiple let in condition

Can you add a way to use multiple "let" in a boolean expression?

fn main() {
    let sa = Some(1);
    let sb = Some(2);

    // Current way:
    if let Some(a) = sa {
        if let Some(b) = sb {
            println!("{}", a + b);
        }
    }

    // New way:
    if let Some(a) = sa &&
       let Some(b) = sb {
        println!("{}", a + b);
    }
}
if let (Some(a), Some(b)) = (sa, sb)

?

:slight_smile:

2 Likes

Right, that's what I use since the beginning :slight_smile: But some people think that's not enough. Today I saw:

https://lambda.xyz/blog/if-chain/

Isn't allowing multiple let a more natural solution?

An example from that page:

fn main() {
    if let ExprCall(ref path_expr, ref args) = expr.node {
        if let Some(first_arg) = args.first() {
            if let ExprLit(ref lit) = first_arg.node {
                if let LitKind::Str(s, _) = lit.node {
                    if s.as_str().eq_ignore_ascii_case("<!doctype html>") {
                        // ...

    // Becomes:
    if let ExprCall(ref path_expr, ref args) = expr.node &&
       let Some(first_arg) = args.first() &&
       let ExprLit(ref lit) = first_arg.node &&
       let LitKind::Str(s, _) = lit.node &&
       s.as_str().eq_ignore_ascii_case("<!doctype html>") {
        // ...
    }
}
1 Like