I ran into a weird compiling error about borrowing and cannot figure it out, the codes are like this:
pub struct Lexer<'a> {
input: String,
token: Token<'a>,
}
#[derive(Debug, Copy, Clone)]
pub struct Token<'a> {
pub t: i32,
pub literal: &'a str,
}
impl<'t, 'l : 't> Lexer<'t> {
pub fn next_token(&'l mut self) {
// compiler error info says -> "`self.token` is borrowed here"
self.read_number();
/* ----------- ERROR ---------------------
cannot assign to `self.token` because it is borrowed
`self.token` is assigned to here but it was already borrowed
--------------------------------------------------
*/
self.token = Token {t : 10, literal: "abc"};
}
fn read_number(&'l mut self) {
}
}
I cannot get it. Function read_number
is just a trivial empty function how can it borrow the self.token
? Does it have something to do with the argument &'l mut self
?