I am working on a Reverse Polish Notation problem. In the eval
function, I want to pass the input tokens as a reference, then iterate through it and using match to do something either A
or B
. But the program produces an error:
expected reference, found enum
main::Token
.
I want to check the type of token here, but Rust doesn't seem to have a straightforward way to print out the type. What is the error source here?
pub enum Operator {
// `+`
Add,
// `-`
Sub,
// `*`
Mul,
}
pub enum Token {
Operator(Operator),
Operand(isize),
}
pub fn eval(tokens: &[Token]) -> Option<isize> {
for token in tokens {
// error here: expected reference, found enum `main::Token`
match token {
Token::Operator(ref a) => println!("do something A"),
Token::Operand(ref b) => println!("do something B"),
};
}
Some(5)
}
fn main() {
// test input
let input1 = Token::Operand(3);
let input2 = Token::Operand(2);
let sign1 = Operator::Add;
let input3 = Token::Operator(sign1);
eval(&[input1, input2, input3]);
}
My question is not a duplicate of Matching on a reference to an enum, since add ref
doesn't save it from the type mismatch error.