The code below compiles, but if the 'args' passed to the function 'f' is changed from a Vec to an array of Strings it does not. I'm trying to understand why. I assume it has something to do with the ownership rules, but I could use some clarification. Thanks.
fn f(args: Box<dyn Iterator<Item=String>>) {
for arg in args {
println!("{}", arg)
}
}
fn main() {
let args = vec!["one_arg".to_string()]; // If changed to array, I get error below
f(Box::new(args.into_iter()));
}
If vec!["one_arg".to_string()]; is changed to: ["one_arg".to_string()];, the error below is the result.
error[E0271]: type mismatch resolving `<std::slice::Iter<'_, std::string::String> as std::iter::Iterator>::Item == std::string::String`
--> src/main.rs:10:7
|
10 | f(Box::new(args.into_iter()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found reference
|
= note: expected struct `std::string::String`
found reference `&std::string::String`
= note: required for the cast to the object type `dyn std::iter::Iterator<Item = std::string::String>`