From:
By default a Box<dyn Trait> doesn't implement the trait of the object it contains. This means that trying to construct PeopleZoo<Box<dyn Person>> won't work out of the box and will give a type error.
Box<dyn Trait>
PeopleZoo<Box<dyn Person>>
Because of this, it's good practice to give a default implementation of your trait for it's boxed counterpart. This can be done by calling as_ref or as_mut on the Box and calling the references relevant method.
as_ref
as_mut
Box
For just a small bit of effort you can help a bunch of people that may consume your struct.
struct PeopleZoo<P: Person> { people: Vec<P>, }
Powered by: C++博客 Copyright © 金慶