I'm new to rust and struggling with this:
use std::iter::Peekable;
use std::str::Chars;
struct Result<'a> {
src: Chars<'a>,
}
impl<'a> Result<'a> {
fn peek_and_consume(&mut self) {
let chars = &mut self.src;
for c in chars {
println!("current: {}", c);
// println!("next: {}", chars.peekable().peek().unwrap());
}
}
fn new(s: &'a str) -> Result<'a> {
Result { src: s.chars() }
}
}
fn main() {
let mut result = Result::new("ABCDEFGHIJK");
result.peek_and_consume();
}
I can't get the following code to build with the commented line un-commented, and I don't understand why. Could someone suggest a way to fix this?
For context: I wanted to store this Chars iterator in the struct so I could easily refer to it in different methods for parsing some complicated structure in my actual code, by advancing with next() and looking forward with peekable.