1

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();
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Akshay Deep Giri
  • 3,139
  • 5
  • 25
  • 33

2 Answers2

5

The function Point::get mutates the Point that it is called on. The function Point::current returns a reference to a part of the Point that it is called on. So, when you write

while let Some(token) = self.current() {
    println!("{:?}", token); 
    let _ = self.get();
}

token is a reference to something stored in self. Because mutating self might change or delete whatever token points to, the compiler prevents you from calling self.get() while the variable token is in scope.

Adrian
  • 14,931
  • 9
  • 45
  • 70
2

@Adrian already gave the correct reason for why the compiler is giving the error message. If you bound the mutating expressions within a scope and then call self.get after the scope completion, you can compile the program.
The code can be modified as

loop{
    {
        let t = if let Some(token) = self.current(){
                    token
                }else{
                    break
                };
        println!("{:?}", t); 
    }
    let b = self.get();
    println!("{:?}", b);
}
basic_bgnr
  • 697
  • 5
  • 14