Using `if let` to check for equality

Also for, and match, and non-global const and static, and closure arguments.

3 Likes

Sure, it's allowed and works fine!

3 Likes

Non-global const and static are different from let since they are items. The names they introduced can be referred before the line they are declared. Besides these "bindings" can't be reassigned.

fn main() {
    dbg!(CONST); // OK
    const CONST: i32 = 123;
    
    dbg!(var); //~ ERROR [E0425]: cannot find value `var` in this scope
    let var = 123;
}

for, match and closure arguments are also different from let that the bindings won't leak into the surrounding scope.

    for x in &[1] {}
    dbg!(x); //~ ERROR [E0425]

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.