use std::collections::HashSet;
fn character_count(needles: &HashSet<char>, needle_type: &str, input: &str) -> i32 {
for needle in needles {
let mut needle_custom;
if needle_type == "double" {
needle_custom = needle.to_string() + &needle.to_string();
} else {
needle_custom = needle.to_string() + &needle.to_string() + &needle.to_string();
}
if input.contains(needle_custom) {
println!("found needle {:?}", needle_custom);
}
}
return 1;
}
error[E0277]: expected a `std::ops::FnMut<(char,)>` closure, found `std::string::String`
--> src/lib.rs:13:18
|
13 | if input.contains(needle_custom) {
| ^^^^^^^^ 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`
The code works if I replace needle_custom
with "test"
.
The
contains
method will accept an&str
or achar
but not aString
.The
contains
method is declared asAnd if you look at the implementors of
Pattern
, you'll see its implemented forchar
and for&str
.This means that you need to pass a
&str
tocontains
instead of your ownedString
. Because&String
coerces to&str
, this is an easy change:Here is your code with this small change working in the Playground.