I'm implementing a parser in Rust. I have to update the index for the lookahead, but when I call self.get()
after self.current()
I get an error:
cannot borrow *self as mutable more than once at a time
It's confusing since I'm new to Rust.
#[derive(Debug)]
pub enum Token {
Random(String),
Undefined(String),
}
struct Point {
token: Vec<Token>,
look: usize,
}
impl Point {
pub fn init(&mut self){
while let Some(token) = self.current(){
println!("{:?}", token);
let _ = self.get();
}
}
pub fn current(&mut self) -> Option<&Token> {
self.token.get(self.look)
}
pub fn get(&mut self) -> Option<&Token> {
let v = self.token.get(self.look);
self.look += 1;
v
}
}
fn main(){
let token_list = vec![Token::Undefined("test".to_string()),
Token::Random("test".to_string())];
let mut o = Point{ token: token_list, look: 0 };
o.init();
}