From: https://nnethercote.github.io/perf-book/heap-allocations.html#reading-lines-from-a-file
BufRead::lines
makes it easy to read a file one line at a time:
use std::io::{self, BufRead};
let mut lock = io::stdin().lock();
for line in lock.lines() {
process(&line?);
}
But the iterator it produces returns io::Result<String>
, which means it allocates for every line in the file.
An alternative is to use a workhorse String
in a loop over BufRead::read_line
:
use std::io::{self, BufRead};
let mut lock = io::stdin().lock();
let mut line = String::new();
while lock.read_line(&mut line)? != 0 {
process(&line);
line.clear();
}
This reduces the number of allocations to at most a handful, and possibly just one.
This will only work if the loop body can operate on a &str
, rather than a String
.