Skip to content

(expected closure, found a different closure) [E0308] #24036

Closed
@richo

Description

@richo

playpen

The error here is extremely unclear to me:

use std::env;

fn main() {
    let args: Vec<_> = env::args().collect();
    let op = match &args[1][..] {
        "foo" => |c: i32| c + 1,
        "bar" => |c: i32| c + 1,
        "baz" => |c: i32| c - 1,
        "other" => |c: i32| c - 1,
        _ => return,
    };
    println!("{:?}", op(11));
}

Not sure if actually a bug in rust (That's my intuition, this looks very valid to me) or just an issue where the error message is incredibly unhelpful.

<anon>:5:14: 11:6 error: match arms have incompatible types:
 expected `[closure <anon>:6:18: 6:32]`,
    found `[closure <anon>:7:18: 7:32]`
(expected closure,
    found a different closure) [E0308]
<anon>:5     let op = match &args[1][..] {
<anon>:6         "foo" => |c: i32| c + 1,
<anon>:7         "bar" => |c: i32| c + 1,
<anon>:8         "baz" => |c: i32| c - 1,
<anon>:9         "other" => |c: i32| c - 1,
<anon>:10         _ => return,
          ...
<anon>:7:18: 7:32 note: match arm with an incompatible type
<anon>:7         "bar" => |c: i32| c + 1,
                          ^~~~~~~~~~~~~~
error: aborting due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions