Why do I get the error “cannot borrow x as mutable more than once”?

前端 未结 2 481
孤独总比滥情好
孤独总比滥情好 2021-01-25 08:11

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:

2条回答
  •  盖世英雄少女心
    2021-01-25 08:44

    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.

提交回复
热议问题