Skip to content

E0631 in closure arguments suggest invalid type parameter #150693

@A4-Tacks

Description

@A4-Tacks

I tried this code:

fn main() {
    "".starts_with(|ch| char::is_ascii(ch));
}

I expected to see this happen: explanation

error[E0631]: type mismatch in closure arguments
    --> src/bin/main.rs:2:8
     |
   2 |     "".starts_with(|ch| char::is_ascii(ch));
     |        ^^^^^^^^^^^ ---- found signature defined here
     |        |
     |        expected due to this
     |
     = note: expected closure signature `fn(_) -> _`
                found closure signature `fn(&_) -> _`
     = note: required for `{closure@src/bin/main.rs:2:20: 2:24}` to implement `Pattern`
note: required by a bound in `core::str::<impl str>::starts_with`
    --> /home/lrne/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/mod.rs:1378:27
     |
1378 |     pub fn starts_with<P: Pattern>(&self, pat: P) -> bool {
     |                           ^^^^^^^ required by this bound in `core::str::<impl str>::starts_with`
help: consider adjusting the signature so it does not borrow its argument
     |
   2 |     "".starts_with(|ch: char| char::is_ascii(ch));
     |                       ++++++

For more information about this error, try `rustc --explain E0631`.
error: could not compile `quick-rs` (bin "main") due to 1 previous error

Instead, this happened: (unexpected change on closure arguments)

error[E0631]: type mismatch in closure arguments
    --> src/bin/main.rs:2:8
     |
   2 |     "".starts_with(|ch| char::is_ascii(ch));
     |        ^^^^^^^^^^^ ---- found signature defined here
     |        |
     |        expected due to this
     |
     = note: expected closure signature `fn(_) -> _`
                found closure signature `fn(&_) -> _`
     = note: required for `{closure@src/bin/main.rs:2:20: 2:24}` to implement `Pattern`
note: required by a bound in `core::str::<impl str>::starts_with`
    --> /home/lrne/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/mod.rs:1378:27
     |
1378 |     pub fn starts_with<P: Pattern>(&self, pat: P) -> bool {
     |                           ^^^^^^^ required by this bound in `core::str::<impl str>::starts_with`
help: consider adjusting the signature so it does not borrow its argument
     |
   2 |     "".starts_with(|char| char::is_ascii(ch));
     |                       ++

For more information about this error, try `rustc --explain E0631`.
error: could not compile `quick-rs` (bin "main") due to 1 previous error

Meta

rustc --version --verbose:

rustc 1.93.0-nightly (f15a7f385 2025-11-04)
binary: rustc
commit-hash: f15a7f38580ddbdc1a23909dd05cf6cc6d9f3919
commit-date: 2025-11-04
host: x86_64-unknown-linux-gnu
release: 1.93.0-nightly
LLVM version: 21.1.3

@rustbot label +A-diagnostics

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in 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