pub struct Lexer {
source: String,
cursor: u64
}
impl Lexer {
pub fn tokenize(&mut self) -> Vec<YamlToken> {
let mut tokens: Vec<YamlToken> = vec![];
while self.cursor < self.source.len() as u64 {
self.cursor += 1;
// Other logic here
}
return tokens;
}
}
I just want to increment the cursor at the start of every iteration. How could I do that without running into the issue of assigning to a borrrowed value?
The code you posted compiles without any errors. I imagine in your actual code you call self.current() and push the result of the call into tokens in some way, which causes &self to be borrowed for the rest of tokenize. One easy way to fix it would be to make current take a &str and u64 instead and call it with (&self.source, self.cursor) so the cursor field doesn't get borrowed.
Friendly reminder unrelated to the borrowing problem at hand: As you are iterating over byte indices of a String, make sure to test whether your code behaves as intended with non-ASCII input, too