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

前端 未结 2 488
孤独总比滥情好
孤独总比滥情好 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:32

    @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);
    }
    

提交回复
热议问题