Loading RustViz Engine...
Evaluate arithmetic expression in postfix notation using pattern matching.
This problem is currently in blueprint template mode. Once the concept foundation is finalized, live in-browser WebAssembly compilation and test assertion runners will be enabled here.
pub fn eval_rpn(tokens: &[&str]) -> i32 {
let mut stack = Vec::new();
for &t in tokens {
match t {
"+" | "-" | "*" | "/" => {
let b = stack.pop().unwrap();
let a = stack.pop().unwrap();
let res = match t {
"+" => a + b,
"-" => a - b,
"*" => a * b,
"/" => a / b,
_ => unreachable!(),
};
stack.push(res);
}
num => stack.push(num.parse::<i32>().unwrap()),
}
}
stack.pop().unwrap()
}