Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mkadziolka opened this issue Jul 21, 2019 · 0 comments · Fixed by #62981
Closed

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

mkadziolka opened this issue Jul 21, 2019 · 0 comments · Fixed by #62981
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mkadziolka
Copy link

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));
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 21, 2019
Centril added a commit to Centril/rust that referenced this issue Jul 25, 2019
Add note suggesting to borrow a String argument to find

Fix rust-lang#62843.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants