The following code works:
fn do_it_fun<I>(words: Vec<&str>, inputs: I)
where I: AsRef<[&'static str]>
{
for word in words {
if inputs.as_ref().into_iter().any(|i| *i == word) {
println!("Match: {}", word);
}
}
}
fn main() {
let input = vec!["foo", "bar"];
let input2 = ["bar", "baz"];
do_it_fun(input, input2);
}
However, instead of trying to use a slice type, if I just use Iterator, the compiler tells me to use dynamic polymorphism:
fn do_it_fun<I>(words: Vec<&str>, inputs: I)
where I: AsRef<IntoIterator<Item=&'static str>>
{
for word in words {
if inputs.as_ref().into_iter().any(|i| *i == word) {
println!("Match: {}", word);
}
}
}
fn main() {
let input = vec!["foo", "bar"];
let input2 = ["bar", "baz"];
do_it_fun(input, input2);
}
Compiling playground v0.0.1 (/playground)
error[E0782]: expected a type, found a trait
--> src/main.rs:2:20
|
2 | where I: AsRef<IntoIterator<Item=&'static str>>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: you can add the `dyn` keyword if you want a trait object
|
2 | where I: AsRef<dyn IntoIterator<Item=&'static str>>
| +++
For more information about this error, try `rustc --explain E0782`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Any ways around this? Using a generic IntoIterator but retaining the possibility of iterating over its references?
AsRef<T>for multipleTs and there are multiple types that can implementIterator<Item = &'static str>which the compiler won't attempt to reconcile (it would ask the caller to explicitly specify what that type is).&[&'static str]gives you&&'static strand not&'static str.