Hi all, I'm new to Rust and coming from Python, never wrote any C or C++ code before. (yes, It's really hard for me but I'm slowly progressing).
Anyways, for learning purposes I started to implement simple language interpreter and reading Orendorff/Blandy book but I'm seeing a pattern where I don't exactly know how to deal with it in Rust:
use crate::ast;
use crate::ast::Program;
use crate::lexer::Lexer;
use crate::token::Token;
struct Parser<'a>{
l: Lexer<'a>,
cur_token: Token<'a>,
peek_token: Token<'a>,
}
impl <'a>Parser<'a> {
fn new(l: Lexer<'a>) -> Parser<'a> {
let mut p:Parser = Parser {
l,
cur_token: Token::new(),
peek_token: Token::new(),
};
p.next_token();
p.next_token();
p
}
fn next_token(&'a mut self) {
self.cur_token = self.peek_token.clone();
self.peek_token = self.l.next_token();
}
fn parse_program() -> ast::Program<'a> {
todo!()
}
}
In Parser::new method, I can't call more than once p.next_token and return the p object. I'm getting:
cannot borrow p
as mutable more than once at a time
How is the best way to deal with such a pattern (when you want to call self method more than once and return the object) in idiomatic Rust?