Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/pattern-matching/exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// ANCHOR: Operation
/// An operation to perform on two subexpressions.
#[derive(Debug)]
#[derive(PartialEq)]
enum Operation {
Add,
Sub,
Expand All @@ -40,18 +41,13 @@ enum Expression {
// ANCHOR: eval
fn eval(e: Expression) -> i64 {
// ANCHOR_END: eval
match e {
Expression::Op { op, left, right } => {
let left = eval(*left);
let right = eval(*right);
match op {
Operation::Add => left + right,
Operation::Sub => left - right,
Operation::Mul => left * right,
Operation::Div => left / right,
}
}
Expression::Value(v) => v,
match e{
Expression::Op{op: a, left: b, right: c} if a == Operation::Add => return eval(*b) + eval(*c),
Expression::Op{op: a, left: b, right: c} if a == Operation::Sub => return eval(*b) - eval(*c),
Expression::Op{op: a, left: b, right: c} if a == Operation::Mul => return eval(*b) * eval(*c),
Expression::Op{op: a, left: b, right: c} if a == Operation::Div => return eval(*b) / eval(*c),
Expression::Value(a) => return a,
_ => return 0,
}
}

Expand Down