A few issues with this:
- It adds a fourth meaning of an identifier in a pattern besides unit struct match, variable binding and match with a static. The case convention would also conflict with bindings, making it locally impossible to judge.
- It makes match depend on a trait (
PartialEq) to compare the equality
- That dependency also means user code can run during matching without an explicit if clause, which so far is being avoided.
- Exhaustiveness checking would have to consider it as a refutable pattern, which means a match on a identifier gets an third exhaustiveness mode (besides “can match and removes this case from all possibilities” and “always matches”)
All in all, it just adds an special case to match and makes it more complicated to reason about, which I think is a bad idea.
However, what might be a bit more viable is to extend match to a syntactic shortform for predicates like this:
let target_char = CHARS[i];
let msg = match byte {
b'0'..b'9' => "numeric",
if @ == target_char => "same char",
_ => "different char",
};
Here, if expr( ... @ ... ) would be the predicate short form and desugar to TMP if expr (... TMP ... )
However, it would be also possible to implement either this or the == special case as a macro:
let target_char = CHARS[i];
let msg = eq_match!{ byte {
b'0'..b'9' => "numeric",
== target_char => "same char",
_ => "different char",
}};
let msg = pshort_match!{ byte {
b'0'..b'9' => "numeric",
if @ == target_char => "same char",
_ => "different char",
}};