Skip to content

Error message for closure that requires |_| is vague #104690

Closed
@schneems

Description

@schneems

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2fecf0a7fbaba8101daba7d1a1ec1837

fn main() {
    let number = 2;
    Some(true).filter({
        if number % 2 == 0 {
            number == 0
        } else {
            number != 0
        }
    });
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): expected a `FnOnce<(&bool,)>` closure, found `bool`
 --> src/main.rs:3:23
  |
3 |        Some(true).filter({
  |   ________________------_^
  |  |                |
  |  |                required by a bound introduced by this call
4 |  |         if number % 2 == 0 {
  |  |_________-
5 | ||             number == 0
6 | ||         } else {
7 | ||             number != 0
8 | ||         }
  | ||_________- this tail expression is of type `_`
9 |  |     });
  |  |_____^ expected an `FnOnce<(&bool,)>` closure, found `bool`
  |
  = help: the trait `for<'r> FnOnce<(&'r bool,)>` is not implemented for `bool`
note: required by a bound in `Option::<T>::filter`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error
Standard Output

Should reference:

|_|

As this is the correct code that compiles:

fn main() {
    let number = 2;
    Some(true).filter(|_| {
        if number % 2 == 0 {
            number == 0
        } else {
            number != 0
        }
    });
}

Using

$ rustc -V
rustc 1.65.0 (897e37553 2022-11-02)

If it helps, I'm trying to find a way to conditionally read in a file only if it exists. I was experimenting with this format:

let blerg = Some(template.target_path.exists()).filter(|_| {
            contents.trim()
                == std::fs::read_to_string(&template.target_path)
                    .unwrap()
                    .trim()
        });

Also worth mentioning in the explanation |_| does not show up at all:

$ rustc --explain E0277 | grep "_"
fn some_func<T: Foo>(foo: T) {
    some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied
fn some_func<T: Foo>(foo: T) {
    some_func(5i32); // ok!
fn some_func<T>(foo: T) {
    some_func(5i32);
fn some_func<T: fmt::Debug>(foo: T) {
    some_func(5i32);
    // some_func(WithoutDebug);

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-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