-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
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.
Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=785ded94c58b6d9a3a8acd16145a4970
fn get_vowel_count(string: &str) -> usize {
string
.chars()
.filter(|c| "aeiou".contains(c))
.count()
}
The current output is:
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.
edmorley
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
Select code repository
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…
8 remaining items