Somewhat unclear error message when match arms evaluate to different types #46776
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
WG-diagnostics
Working group: Diagnostics
Hi, I just found a minor annoyance in the compiler diagnostics.
Here's the code:
keyboard.data is a HashMap inside a struct, defined as:
Keyboard::assoc_key is defined as:
Keys
is just a simple enum type.The problem with the code above is that
HashMap::insert
returns anOption
and so the match tries to evaluate to that type which is, however, not needed in this case. But it means that the default arm must also evaluate to anOption
which it doesn't.The compiler obviously complains about this:
This confused me for a while because that was the first time I used a
HashMap
and I didn't have in mind thatHashMap::insert
returns something.I think the compiler should specifically mention why it expects an
Option
, that is, from where it inferred the type which would have made it more obvious thatHashMap::insert
returns anOption
.I suppose this is something that typically doesn't happen to someone who is familiar enough with the language but it's confusing for someone who only recently began learning Rust.
I've looked for existing similar issues but found nothing obvious. If this has already been discussed somewhere or is not considered an issue, please close this :)
The text was updated successfully, but these errors were encountered: