Skip to content

E0308 hint suggests adding a return type to a closure #128561

Description

@lolbinarycat

Code

fn main() {
    b"abc".iter().for_each(|x| dbg!(x))
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:2:32
  |
2 |     b"abc".iter().for_each(|x| dbg!(x))
  |                               -^^^^^^^ expected `()`, found `&u8`
  |                               |
  |                               help: try adding a return type: `-> &u8`
  |

Desired output

error[E0308]: mismatched types
 --> src/main.rs:2:32
  |
2 |     b"abc".iter().for_each(|x| dbg!(x))
  |                               -^^^^^^^ expected `()`, found `&u8`
  |                               |
  |                               help: try ignoring this value: `_ = dbg!(x)`

Rationale and extra context

closures have their return type inferred if not specified, so adding a return type to a closure will never fix a type mismatch error.

Other cases

No response

Rust Version

1.80.0

Anything else?

No response

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 Aug 2, 2024
  2. self-assigned this
    on Aug 3, 2024
  3. added a commit that references this issue on May 22, 2025
    3529341
  4. added a commit that references this issue on May 23, 2025
    10d74e6
  5. chenyukang commented on May 23, 2025

    @chenyukang
    Member

    This issue should be reopen, because of comment:
    #135562 (comment)

  6. removed their assignment
    on Aug 6, 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