While loop for reading tokens


Hello, I’m writing a reverse polish notation evaluator in Rust and input will be a postfix expression such as operator or operand and returns some(value) if it is valid and none if it’s invalid

This is the algorithm.
While there are input tokens left
Read the next token from input.
If the token is a value
Push it onto the stack.
Otherwise, the token is an operator (operator here includes both operators and functions).
It is already known that the operator takes n arguments.
If there are fewer than n values on the stack
(Error) The user has not input sufficient values in the expression.
Else, Pop the top n values from the stack.
Evaluate the operator, with the values as arguments.
Push the returned results, if any, back onto the stack.
If there is only one value in the stack
That value is the result of the calculation.
Otherwise, there are more values in the stack
(Error) The user input has too many values.

My questions is would the while loop look like this?

while scan.hasnext(){


It seems that you try to fit code patterns from some other language into Rust. More idiomatic solution than a while loop would be to let the tokenizer return an Iterator, so you can use the for loop:

for token in tokens { ... }

Eg. if you just want to split tokens on whitespace, you can do the following:

for token in input_string.split_whitespace() { ... }

PS. If you’ll paste Rust code like below, it’ll get nicely formatted:

fn main() {}