pub struct Interpreter<'a> {
it:&'a Vec<char>,
}
impl<'a> Interpreter<'a> {
pub fn new(infix: &str) -> Self {
let m = infix.chars().into_iter().collect()
Self { it: m }
}
pub fn interpret(&mut self, out: &mut String) {
self.term(out);
while let Some(&op) = self.next_char() {
if op == '+' || op == '-' {
self.term(out);
out.push(op)
} else {
// panic!("Unexpected symbol '{}'", op)
}
}
}
fn next_char(&mut self) -> Option<&char> {
let m = self.it.iter().next();
self.it.remove(0);
return m;
}
fn term(&mut self, out: &mut String) {
match self.next_char() {
Some(&ch) if ch.is_digit(10) => out.push(ch),
Some(&ch) => panic!("Unexpected symbol '{}'", ch),
None => panic!("Unexpected end of string"),
}
println! {">>>>>>{}", out}
}
}
fn main() {
let mut intr = Interpreter::new("2+3");
let mut postfix = String::new();
intr.interpret(&mut postfix);
assert_eq!(postfix, "23+");
// intr = Interpreter::new("1-2+3-4");
// postfix.clear();
// intr.interpret(&mut postfix);
// assert_eq!(postfix, "12-3+4-");
}
error: Compiling helloworld v0.1.0 (/Users/wuls/Desktop/rust/helloworld)
error[E0277]: a value of type &Vec<char>
cannot be built from an iterator over elements of type char
--> src/main.rs:601:32
|
601 | let m = infix.chars().collect();
| ^^^^^^^ value of type &Vec<char>
cannot be built from std::iter::Iterator<Item=char>
|
= help: the trait FromIterator<char>
is not implemented for &Vec<char>
error: aborting due to previous error
For more information about this error, try rustc --explain E0277
.
error: could not compile helloworld