struct Lexer<'chars> {
chars: Option<Peekable<Chars<'chars>>>,
vanish: bool
}
fn chomp(&mut self,stream: &mut Peekable<Chars<'chars>>) -> Option<char> {
return loop {
match stream.next() {
None => {
self.vanish = true;
return None;
}
Some(valid) => {
if !valid.is_whitespace() {
break Some(valid);
}
continue;
}
}
};
}
fn next(&mut self) -> Option<Token> {
if !self.ensure() {
return None;
}
let mut stream = self.chars.as_mut().unwrap();
let mut ch = '0';
ch = self.chomp(&mut stream).unwrap();
error[E0499]: cannot borrow `*self` as mutable more than once at a time
--> source_file.rs:59:10
|
56 | let mut stream = self.chars.as_mut().unwrap();
| ---------- first mutable borrow occurs here
...
59 | ch = self.chomp(&mut stream).unwrap();
| ^^^^ second mutable borrow occurs here
...
78 | }
| - first borrow ends here
In my first attempts at learning Rust I came across two major obstacles, lifetime and the rule of borrowing mutable references. I would like to ask for help to get around this problem of borrowing self as mutable. In the code in question I cache the Peekable object and when I use self to call a method the compiler does not accept it. I believe that it does not allow because I can change the Peekable or even delete it making the previous cache invalid. But shouldn't Peekable's lifetime and compiler intelligence be enough to know that this won't be the case and allow me to make the call?
Extra: is my chomp function acceptable or is it written in a weird way overusing the language flexibility?