Just started with Rust. Would like to create a setter that will accept trait as param. To illustrate idea
pub trait Engine {
fn start() -> bool;
}
struct Car {
engine: Box<dyn Engine>,
}
impl Car {
pub fn new() -> Self {
let engine = Box::new(DummyEngine {});
Self {
engine,
}
}
pub fn set_engine(&mut self, engine: &dyn Engine) {
self.engine = Box::new(engine);
}
}
The setter code complains:
the trait bound `&dyn Engine: Engine` is not satisfied
required for the cast to the object type `dyn Engine` rustcE0277
Also, how can the dummy default engine be avoided? Let's say a car doesn't need an engine. Should it be wrapped in an Option?