Skip to content

Confusing error message when trying to use String as a Pattern #62843

Closed
@mkadziolka

Description

@mkadziolka

Consider this code (playground):

fn main() {
    let line = String::from("abc");
    let pattern = String::from("bc");
    println!("{:?}", line.find(pattern));
}

On both current stable and nightly, this results in this error message:

error[E0277]: expected a `std::ops::FnMut<(char,)>` closure, found `std::string::String`
 --> src/main.rs:4:27
  |
4 |     println!("{:?}", line.find(pattern));
  |                           ^^^^ expected an `FnMut<(char,)>` closure, found `std::string::String`
  |
  = help: the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String`
  = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `std::string::String`

error: aborting due to previous error

This is really confusing, since the proper way to get this to compile is to use a reference instead of an owned string:

-    println!("{:?}", line.find(pattern));
+    println!("{:?}", line.find(&pattern));

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.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