I have a trait Foo, and concrete types A and B are both bounded by the trait Foo. I want to return a Vec<Foo>
, where Foo could be either concrete type A or B, like shown below:
trait Foo { }
pub struct A {}
pub struct B {}
impl Foo for A {}
impl Foo for B {}
fn test() -> Vec<Foo> {
let generic_vec: Vec<Foo> = Vec::new();
generic_vec.push(A {});
generic_vec.push(B {});
return generic_vec;
}
The compiler at the moment is throwing the error that the sized trait is not implemented for Foo. I could wrap Foo in a Box, but I don't want to return a Vec of trait objects because of the runtime overhead that they impose.
I was wondering if there was some Rust Generics feature that would allow me to return a Vec of generic types without having to use trait objects.