Skip to content

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

@ethercrow

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

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
osa1

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)
}
ethercrow

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.

edmorley

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.

estebank

estebank commented on Mar 26, 2022

@estebank
Contributor

Addressing in #91873

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

added 2 commits that reference this issue on Apr 4, 2022
6137798
a5c8169

8 remaining items

Loading
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

      Participants

      @ethercrow@osa1@edmorley@estebank

      Issue actions

        Using str::contains inside of a filter leads to confusing error message. · Issue #87437 · rust-lang/rust