Hello again,
first the enum in question:
pub enum Exp {
Int {
val: i32,
b: bool
},
Plus {
e1: Box<Exp>,
e2: Box<Exp>,
b: bool
},
Mult{
e1: Box<Exp>,
e2: Box<Exp>,
b: bool
},
}
In the following pretty function I want to set the bool b of e1 and e2 to true whenever the Multiplication has been visited. (so for example (1 + 2) * 2 keeps it paranthesis but 1 + 2 * 2 stays the same too).
pub fn pretty(mut self) -> String {
return match self {
Exp::Int { val, mut b } => {
val.to_string()
},
Exp::Plus { e1, e2, mut b } => {
println!("b: {}", b);
if b { //if true Mult has been visited
let s = format!("s: ( {} + {} )", e1.pretty(), e2.pretty());
s.to_string()
}
else { //if false Mult has not been visited
let s = format!("s: {} + {}", e1.pretty(), e2.pretty());
s.to_string()
}
},
Exp::Mult { mut e1, mut e2, mut b } => {
e1.set(true);
e2.set(true);
let s = format!( "{} * {}", e1.pretty(), e2.pretty());
s.to_string()
}
So by calling set on e1 and e2 I thought it would change it:
pub fn set(&mut self, set_to: bool) -> (){
return match self{
Exp::Int { val, mut b} => {
b = set_to
}
Exp::Plus { e1, e2, mut b} => {
println!("plus");
b = set_to
}
Exp::Mult { e1, e2, mut b} => {
println!("mult");
//b = set_to
}
}
}
My question:
Is it even correct, that when I call set on e1 (or e2) e1 is the self given to set() as a mutable reference including all it's fields?
Because when I looked into it with the Debugger all the expressions where in their correct places. It even prints the correct output just without the parenthesis. And when I initially set the bool in the ::Plus expressions to true it correctly prints the "unnecessary" parenthesis too. So the error must be in how I try to access the bool fields.
Kind regards,
naili