Skip to content

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

Closed

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

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jul 24, 2021
  2. osa1 commented on Aug 14, 2021

    @osa1
    Contributor

    and also help me to add a missing ampersand.

    Do you mean a missing star? Working version:

    fn get_vowel_count(string: &str) -> usize {
            string
            .chars()
            .filter(|c| "aeiou".contains(*c))
            .count()
    }

    I agree that the error message is very misleading.

    Smaller repro:

    fn test(c: &char) -> bool {
        "aeiou".contains(c)
    }
  3. ethercrow commented on Aug 14, 2021

    @ethercrow
    Author

    Do you mean a missing star?

    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.

  4. edmorley commented on Feb 16, 2022

    @edmorley
    Contributor

    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.

  5. estebank commented on Mar 26, 2022

    @estebank
    Contributor

    Addressing in #91873

    Screen Shot 2022-03-26 at 3 38 34 PM

  6. added 2 commits that reference this issue on Apr 4, 2022
    6137798
    a5c8169
  7. added 2 commits that reference this issue on Apr 5, 2022
    3e15b92
    ecb0a11
  8. added 2 commits that reference this issue on Jan 1, 2025
  9. added a commit that references this issue on Jan 1, 2025
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