You can use the peekable
adapter.
This requires switching to manual iteration (a while let
loop and explicitly calling next
), but it lets you peek()
the iterator to see if there is a next item. If there isn't, you know it's the last line (at least for the more fusable iterators).
let file = File::open(&files[index])
.map_err(|e| format!("Could not open log file: {}", e))?;
let reader = BufReader::new(file);
let mut iterator = reader.lines().enumerate().peekable();
while let Some((index, line)) = iterator.next() {
if line == "" && iterator.peek().is_none() {
break;
}
// do something with the line...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…