Hey guys!
I think my current understanding of trait objects is slightly wrong. I have already read the chapter dedicated to them in the rust book twice but for some reason it's still not apparent to me.
Why is "this" code not working but the "other" code is:
this code
pub trait MyTrait {}
pub struct MyStruct;
impl MyTrait for MyStruct {}
fn main() {
// I am not referring to the size problem but to the
// "expected trait object `dyn MyTrait`, found struct
// `MyStruct`" error I'm getting.
let x: dyn MyTrait = MyStruct {};
}
other code
pub trait MyTrait {}
pub struct MyStruct;
impl MyTrait for MyStruct {}
fn main() {
let x: Box<dyn MyTrait> = Box::new(MyStruct {});
}
Best regards,
Dominik