trait MyTrait {
fn my_f(&self) ;
}
struct A {}
struct B {}
impl MyTrait for A {
fn my_f(&self) {
println!("A.my_f()");
}
}
impl MyTrait for B {
fn my_f(&self) {
println!("B.my_f()");
}
}
struct MyList<T: MyTrait> {
list: Vec<T>
}
fn main() {
let list = MyList {
list: vec![
Box::new(A{}),
Box::new(B{})
]
};
}
I followed this article, Using Trait Objects That Allow for Values of Different Types
According to this article:
This restricts us to a Screen instance that has a list of components all of type Button or all of type TextField. If you’ll only ever have homogeneous collections, using generics and trait bounds is preferable because the definitions will be monomorphized at compile time to use the concrete types.
On the other hand, with the method using trait objects, one Screen instance can hold a Vec that contains a Box as well as a Box. Let’s look at how this works, and then we’ll talk about the runtime performance implications.
But this code can't be compiled.