Loop-match, for-match

Well, if and while don't bind anything, so this doesn't really apply there. For functions, well, at least for single argument closures one could use something like |match| { /* match arms */}. This has some precedent in Haskell, I've explained it in more detail in this post in a thread that was also about eliminating extra indentation levels:


Thinking about it again now, one could even consider supporting e. g. |match, match, match| { (a, b, c) => {...} }, bundling up multiple values in a tuple.

And one could go much further and generalize this to allowing match as a pattern in general in some places, e. g.

while let Some(match) = foo.next() {
    0 => {
        bar1();
        bar2();
    }
    n => baz(n),
}

and similar for if let and even function definitions

fn foo(match: Option<bool>, u: char, (match, i): (i32, u8)) {
    (Some(true), 0) => bar(), 
    (None, n) if n > 0 => baz(), 
    (_, n) => qux(), 
}

Edit: Going back to discussing your suggestions, this generalization would mean the for syntax stays as you proposed, and the loop match expr { ... } needs to be written as while let match = expr { ... }. Admitted, that's a bit of a keyword zoo, and a bit too while true-style, so perhaps loop match could be a new “abbreviation” of while let match =.

2 Likes