You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using str::contains inside of a filter leads to confusing error message. #87437
I've been trying to learn some Rust by doing CodeWars. I've spent some time being stuck on this diagnostic. One of the easiest exercises there is to count vowels in a string. First I did it with a loop, but then got stuck trying to rewrite is as a filter.
error[E0277]: expected a `Fn<(char,)>` closure, found `char`
--> src/lib.rs:4:38
|
4 | .filter(|c| "aeiou".contains(c))
| ^ expected an `Fn<(char,)>` closure, found `char`
|
= help: the trait `Fn<(char,)>` is not implemented for `char`
= note: required because of the requirements on the impl of `FnOnce<(char,)>` for `&char`
= note: required because of the requirements on the impl of `Pattern<'_>` for `&char`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
error: could not compile `challenge`
Ideally the output should not lead with "expected an Fn" and also help me to add a missing ampersand.
Both |c| "aeiou".contains(*c) and |&c| "aeiou".contains(c) are accepted. At the time of writing the ticket I found only the latter way to fix the error, that's why I said ampersand.
Activity
osa1 commentedon Aug 14, 2021
Do you mean a missing star? Working version:
I agree that the error message is very misleading.
Smaller repro:
ethercrow commentedon Aug 14, 2021
Both
|c| "aeiou".contains(*c)and|&c| "aeiou".contains(c)are accepted. At the time of writing the ticket I found only the latter way to fix the error, that's why I said ampersand.edmorley commentedon Feb 16, 2022
This misleading diagnostic also caught me out too recently.
In my case the usage was virtually identical to the OP's:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=43fbdf70f0270c762d0558fdb34ae1f3
I also found #90970 which seems related.
estebank commentedon Mar 26, 2022
Addressing in #91873
Fix rust-lang#90970, doesn't address rust-lang#87437
Suggest dereferncing when possible in E0277, fix rust-lang#87437
Rollup merge of rust-lang#91873 - estebank:mention-impls-for-unsatisf…
Rollup merge of rust-lang#91873 - estebank:mention-impls-for-unsatisf…
Revert "Suggest dereferncing when possible in E0277, fix rust-lang#87437
Revert "Fix rust-lang#90970, doesn't address rust-lang#87437"
Rollup merge of rust-lang#133292 - dianne:e0277-suggest-deref, r=este…
Rollup merge of rust-lang#133292 - dianne:e0277-suggest-deref, r=este…
Unrolled build for rust-lang#133292