Thank you. I am trying to get my feet wet, and I am basically trying to reimplement some of my python code in Rust.
I am trying to write a calculator, where the first step is to parse user input into tokens, e.g, numbers, operations, blank space, etc:
trait Token {
fn append(&mut self, symbol: String) -> bool;
fn new() -> Self where Self: Sized;
}
struct Operation {
symbols: Vec<String>,
}
impl Operation {
const ALLOWED_VALUES: &'static [&'static str] = &["*", "/", "+", "-", "**"];
}
impl Token for Operation {
fn new() -> Self {
Operation {symbols: vec![]}
}
fn append(&mut self, symbol: String) -> bool {
// if this symbol makes a valid operation together with the previously added ones, add it to self.symbols, return true, otherwise return false
}
}
Then I want to iterate over the symbols of the user input and try to add it to all of the possible tokens. At the first iteration, I try to add it to all tokens known to my programme, and keep only those ones that could add the symbol to themselves, then I will add the symbol to the remaining tokens and remove the ones that couldn't consume the symbol. When I'm left with one token, and that token stops accepting new symbols, I will deem it complete.
For that I need a way to produce new token instances. I am trying to use traits, but I run into errors:
trait Factory<T> {
fn make(&self) -> T;
}
struct OperationFactory {}
impl Factory<Operation> for OperationFactory{
fn make(&self) -> Operation {
return Operation { symbols: vec![] };
}
}
fn main() {
let known_token_types: Vec<dyn Factory<dyn Token>> = vec![SpaceFactory{}, OperationFactory{}];
}
the size for values of type `dyn Factory<dyn Token>` cannot be known at compilation time
the trait `Sized` is not implemented for `dyn Factory<dyn Token>`rustcE0277
mod.rs(400, 16): required by a bound in `Vec`
As for the fancy stuff, I am not ready for that yet )