- I have code that reduces ot the following:
pub trait AnimalT {
fn num_legs(&self) -> isize;
}
pub struct Cat {}
impl AnimalT for Cat {
fn num_legs(&self) -> isize { unimplemented!() }
}
pub struct Wrapper<'a, T: ?Sized> {
pub animal: &'a T,
}
pub fn foo<'a>(animal: Wrapper<'a, AnimalT>) {
}
#[test]
fn test() {
let c = Cat{};
let f: Wrapper<Cat> = Wrapper{ animal: &c };
foo(f);
}
- I get the error of:
|
26 | foo(f);
| ^ expected trait AnimalT, found struct `Cat`
|
= note: expected type `Wrapper<'_, (dyn AnimalT + 'static)>`
found type `Wrapper<'_, Cat>`
-
This is confusing to me because we have
impl AnimalT for Cat
so I see no reason why a&'a AnimalT
can not store a&'a Cat
-
What am I doing wrong, and how do we fix this?