How to Read a Text File Line by Line Efficiently in Rust?

Photo by Poppy Thomas Hill on Pexels.com

How to open a file for reading?

In Rust, a reference to an open file on the filesystem is represented by the struct std::fs::File. And we can use the File::open method to open an existing file for reading:

pub fn open<P: AsRef<Path>>(path: P) -> Result<File>

Which takes a path (anything it can borrow a &Path from i.e. AsRef<Path>, to be exact) as a parameter and returns an io::Result<File>.

How to read a file line by line efficiently?

For efficiency, readers can be buffered, which simply means they have a chunk of memory (a buffer) that holds some input data in memory. This saves on system calls. In Rust, a BufRead is a type of Read which has an internal buffer, allowing it to perform extra ways of reading. Note that File is not automatically buffered as File only implements Read but not BufRead. However, it’s easy to create a buffered reader for a File:

BufReader::new(file);

Finally, we can use the std::io::BufRead::lines() method to return an iterator over the lines of this buffered reader:

BufReader::new(file).lines();

Put everything together

Now we can easily write a function that reads a text file line by line efficiently:

use std::fs::File;
use std::io::{self, BufRead, BufReader, Lines};
use std::path::Path;

fn read_lines<P>(path: P) -> io::Reasult<Lines<BufReader<File>>>
where P: AsRef<Path>,
{
    let file = File::open(path)?;
    Ok(BufReader::new(file).lines())
}

Want to buy me a coffee? Do it here: https://www.buymeacoffee.com/j3rrywan9

Leave a comment