trait A {
fn a(&self) -> bool;
}
struct B {}
struct C {}
impl A for B {
fn a(&self) -> bool {
true
}
}
impl A for C {
fn a(&self) -> bool {
false
}
}
struct D<T: A> {
x: Vec<Box<T>>,
}
fn main() {
let v: Vec<Box<A>> = vec![Box::new(B {}), Box::new(C {})];
assert_eq!(2, v.len());
let mut d = D { x: vec![] };
d.x.push(Box::new(B {}));
d.x.push(Box::new(C {}));
assert_eq!(2, d.x.len());
}
error[E0308]: mismatched types
--> <anon>:29:23
|
29 | d.x.push(Box::new(C {}));
| ^^^^ expected struct `B`, found struct `C`
|
= note: expected type `B`
found type `C`
Hi everyone – I'm struggling to understand why the two cases above are treated differently, and how I can give the compiler enough info to understand that the second case (vector inside struct) is okay. Do I need another type annotation somewhere?
Thanks in advance for any help.