Is it possible to collect a Vec<&dyn Trait>
from an iterator of values implementing Trait
?
Here is an example, based on the Vector of objects belonging to a trait question:
trait Animal {
fn make_sound(&self) -> String;
}
struct Dog;
impl Animal for Dog {
fn make_sound(&self) -> String {
"woof".to_string()
}
}
fn main() {
let dogs = [Dog, Dog];
let v: Vec<&dyn Animal> = dogs.iter().collect();
for animal in v.iter() {
println!("{}", animal.make_sound());
}
}
this fails with error[E0277]: a value of type "Vec<&dyn Animal>" cannot be built from an iterator over elements of type
&Dog`
however, if you use push the dogs individually into the vec (like in the answer to the original question) it works without issues.
let dog1: Dog = Dog;
let dog2: Dog = Dog;
let v: Vec<&dyn Animal> = Vec::new();
v.push(&dog1);
v.push(&dog2);