Hi, I'm working on a Sudoku solver, where I want to have a list of strategies which will be applied one after another.
I don't know how to put them into a Vector, so that I can later iterate over it and call each of them. My code is:
pub struct SolvingStep {
}
pub trait Strategy {
fn find_step(&self) -> Option<SolvingStep>;
}
pub struct S01SimpleNaked {
}
impl S01SimpleNaked {
pub fn new() -> Self {
Self{}
}
}
impl Strategy for S01SimpleNaked {
fn find_step(&self) -> Option<SolvingStep> {
todo!()
}
}
pub struct S02HiddenSingle {
}
impl S02HiddenSingle {
}
impl Strategy for S02HiddenSingle {
fn find_step(&self) -> Option<SolvingStep> {
todo!()
}
}
pub struct SudokuSolver {
strategies: Vec<dyn Strategy>
}
impl SudokuSolver {
pub fn new() -> Self {
Self {
strategies: vec![S01SimpleNaked{}, S02HiddenSingle{}]
}
}
}
fn main() {
}
and I'm getting following compilation error:
Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `(dyn Strategy + 'static)` cannot be known at compilation time
--> src/main.rs:38:17
|
38 | strategies: Vec<dyn Strategy>
| ^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Strategy + 'static)`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
Code is here:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8bb5ed960ecdd1fd15e0aad552d0ed74
How can I put "trait instances" into one vector? Or do I simply need to provide Sized as compiler is suggesting? How can that be done? Many thanks!