How could I pack the following code into a single iterator?
use std::io::{BufRead, BufReader};
use std::fs::File;
let file = BufReader::new(File::open("sample.txt").expect("Unable to open file"));
for line in file.lines() {
for ch in line.expect("Unable to read line").chars() {
println!("Character: {}", ch);
}
}
Naively, Iād like to have something like (I skipped unwraps)
let lines = file.lines().next();
Reader {
line: lines,
char: next().chars()
}
and iterate over Reader.char
till hitting None
, then refreshing Reader.line
to a new line and Reader.char
to the first character of the line. This doesn't seem to be possible though because Reader.char
depends on the temporary variable.
Please notice that the question is about nested iterators, reading text files is used as an example.