Currently, one can omit the comma at the end of a match
arm if the RHS of the =>
is a bare block expression:
match foo {
Foo => { ... } // no comma!
Bar => { ... }
}
I’ve often wanted to modify this rule to make it more consistent with how expressions and statements work. The motivation is that there are certain types of expressions which when used in statement position do not require semicolons for separation. For example, the following:
if foo { bar; };
loop { baz(); };
{ qux(); };
match something { ... };
can be rewritten more idiomatically as:
if foo { bar; }
loop { baz(); }
{ qux(); }
match something { ... }
This ability to remove semicolons does not apply to all expressions—only certain ones that end in }
(if
/else
, while
, loop
, for
, match
, bare blocks, macros invoked like foo! { ... }
, and probably some other things I’ve forgotten). The change I’d like to make to match
arms would be to extend this rule to comma elision. So under this change, the following would be valid:
match foo {
Foo => if foo {
bar
} else {
baz
}
Bar => loop {
baz();
}
Baz => {
qux();
}
Qux => match something {
...
}
}
Unfortunately, this is a breaking change. Edit: turns out it’s actually not a breaking change! See my comment below. The rest of the comment is left here for posterity. However, it’s a relatively minor one—the only code that would be broken by a careful implementation would be code similar to the following:
match foo {
Foo => if foo { some_function } else { some_other_function }(),
Bar => if foo { some_array } else { some_other_array }[],
Baz => if foo { some_int } else { some_other_int } & 1,
Qux => if foo { some_bool } else { some_other_bool } && some_predicate(),
}
where a block-like expression immediately following the =>
of a match
arm is followed by a token that can also start a pattern. As far as I know, the only such tokens are &
, &&
, (
, and [
. From this I would estimate that virtually no real code would be broken by this change, and any code that would get broken could easily be fixed in a manner that would let it continue to compile on older versions of Rust.