Here's a function that compiles:
fn compile() -> bool {
let word = String::from("hello");
"hello world".contains(&word)
}
Good. Now, if I omit passing a reference to contains and pass the actual String:
fn wont_compile() -> bool {
let word = String::from("hello");
"hello world".contains(word) // <- missing "&" here before "word"
}
, then compilation fails with error
error[E0277]: expected a `std::ops::FnMut<(char,)>` closure, found `std::string::String`
--> src/lib.rs:96:19
|
96 | "hello world".contains(word)
| ^^^^^^^^ 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`
Usually rustc is pretty helpful, but here I have zarroo idea of what it's trying to tell me and, assuming the solution, I don't understand how a String isn't a std::ops::FnMut<(char,)>, but a reference to a String is. What is a closure even doing here? The only reason I found my error was that I looked up a contains example from the rust book. Can you show me how I c/should have jumped that hoop properly?
-
Using rustc output: what's going on with the above-quoted error message (or in
rustc --explain E0277) ? What should have put me on the right track and I missed? -
Using the docs: docs for str.contains say I should pass a Pattern. What here explicits that a
Stringisn't aPattern?
Finally, why doesn't / couldn't the String type implement the Pattern trait, and why does a reference to a String can?
Thanks for the help
.