Skip to content

Using str::contains inside of a filter leads to confusing error message. #87437

Description

@ethercrow

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions