Reputation: 19
I've recently taken on learning Rust and I'm trying to write a small expression evaluator. I've been practicing Rust for a few days now and thought this task would be cool to work with Rust's Traits. What I tried to do is make Sum & Number structs implement Expression trait, so that I could express (pun unintended) (1 + 2) as an expression where left and right hand sides are expressions too. I've stumbled onto the problem that you can't just use Traits as properties' types, so you instead should use &dyn Trait or Box in the Book. Following this notion a rewrote it and now it compiles, but I can't get access to values inside Sum. Here's my code:
trait Expression {}
#[derive(Debug)]
struct Number {
pub val: i32
}
impl Expression for Number {}
struct Sum {
pub left: Box<dyn Expression>,
pub right: Box<dyn Expression>
}
impl Expression for Sum {}
fn main() {
let e = Sum{ left: Box::new(Number{ val: 2}),
right: Box::new(Number{ val: 2})
};
let sum = (2 + 2);
println!("{:#?}", sum);
}
What I want to be able to do is get to Number's value:
e.left.val
and use nested constuctions like:
Sum{Sum{Number, Sum{Number, Number}}, Number}
I also tried to make explicit cast to Number:
let val = (e.left as Number).val;
But it fails with an error:
non-primitive cast: std::boxed::Box<(dyn Expression + 'static)>
as Number
note: an as
expression can only be used to convert between primitive types. Consider using the From
trait.
Sorry for any language mistakes or messy explanation, English is not my first language.
I'm not an experienced programmer and very new to Rust so I would really appreciate any help, thanks!
Upvotes: 2
Views: 136
Reputation: 5397
I think what you're trying to do is this (complete code in the playground):
trait Expression {
fn evaluate(&self) -> i32;
}
impl Expression for Number {
fn evaluate(&self) -> i32 {
self.val
}
}
impl Expression for Sum {
fn evaluate(&self) -> i32 {
self.left.evaluate() + self.right.evaluate()
}
}
Upvotes: 0
Reputation: 17
Rust doesn't let you cast non primitive types.
Reference: https://doc.rust-lang.org/rust-by-example/types/cast.html
Upvotes: 0