To avoid certain unstable API I have been trying to make use of CharIndices in my lexer for Scheme R7RS. I had managed to avoid certain dreaded lifetime/borrowing issues, but now I can no longer run away from my ignorance. I feel like this is the one great struggle I have with Rust, and if I can finally understand this, all will be well. Below is a very simple piece of code that does not compile.
use std::str::CharIndices;
struct Lexer<'a> {
iter: CharIndices<'a>,
}
impl<'a> Lexer<'a> {
fn new(input: String) -> Lexer<'a> {
let ci = input.char_indices();
Lexer {
iter: ci
}
}
}
fn main() {
Lexer::new("input string".to_string());
}
I have tried all sorts of things, but none of them compile. My goal is simple: have a lexer that uses an instance of CharIndices to move forward and backward through the input text.
Any tips on where I’ve gone wrong? I’ve read the Rust book, the reference, just about every Rust tutorial I could find, as well as the excellent Rust by Example book. I sort of think I know what’s going on, but I can’t for the life of me find a solution.
Thanks