My Rust program is intented to read a very large (up to several GB), simple text file line by line. The problem is, that this file is too large to be read at once, or to transfer all lines into a Vec<String>
.
What would be an idiomatic way to handle this in Rust?
You want to use the buffered reader,
BufRead
, and specifically the functionBufReader.lines()
:Note that you are not returned the linefeed, as said in the documentation.
If you do not want to allocate a string for each line, here is an example to reuse the same buffer:
Playground
Or if you prefer a standard iterator, you can use this
Rc
trick I shamelessly took from Reddit:Playground