Read File Line by Line In Rust

Kuldeep Singh
Dev Genius
Published in
5 min readApr 23, 2023

--

Rust is a modern and fast programming language designed for performance, memory safety and concurrency. Rust offers various functions and methods for file handling, Including the ability to read files line by line. In this article, We’ll explore how to read a file line by line in Rust.

File Handling in Rust

Before we dive into reading files line by line in Rust, Let’s first take a look at how Rust handles file operations. Rust has built-in support for file handling, and it provides a standard library std::fs for file operations. This library includes functions for opening, reading and writing files, and other things.

Opening Files in Rust

Before reading the file we should know how to open a file in Rust. We can use the std::fs::File::open method to open a file in Rust. Here’s how to open a file named “example.txt” for reading:

use std::fs::File;

fn main() -> Result<(), std::io::Error> {
let file = File::open("example.txt")?;
print!("{:?}",file);
Ok(())
}

The File::open method returns a Result, Which is an enum data type that represents either success or failure of a file operation. The main function will also return a Result<(), std::io::Error> as well, Which allows to handle any potential errors returned by File::open method.

Closing the Files in Rust

Now that we know how to open, write and create to a file, We need to also close it to free up system resources. Rust’s std::fs::File type implements the Drop trait, which means that the drop method is automatically called when the file object goes out of scope. Here’s an example of closing a file in Rust:

use std::fs::File;

fn main() -> Result<(), std::io::Error> {
let file = File::open("example.txt")?;

// ... read or write to the file ...

drop(file);
Ok(())
}

Note that the method is called implicitly when the object goes out of scope, so we don’t need to call it manually in most cases.

Reading a File Line by Line in Rust

Now that we know how to open and close files, Let’s explore how to read a file line by line in Rust. Rust provides a type that wraps a object and provides buffered reading of the file. Using a can improve the efficiency of reading a file line by line, especially for larger files.

Using BufReader for Efficient File Reading

Here’s an example of how to use BufReader to read a file line by line in :

use std::fs::File;
use std::io::{BufRead, BufReader};

fn main() -> Result<(), std::io::Error> {
let file = File::open("example.txt")?;
let reader = BufReader::new(file);

for line in reader.lines() {
println!("{}", line?);
}

Ok(())
}

In this example, We first opened the file using File::open method, Then we wrapped it in a BufReader using the BufReader::new method. The BufReader reads the file in and stores them in a buffer for faster access.

The BufRead trait also provides a read_line method that reads a line of text from the buffer and stores it in a String. Here is another example of reading file line by line in Rust using this new method read_line:

use std::fs::File;
use std::io::{BufRead, BufReader};

fn main() -> Result<(), std::io::Error> {
let file = File::open("example.txt")?;
let mut reader = BufReader::new(file);

let mut line = String::new();
loop {
let bytes_read = reader.read_line(&mut line)?;
if bytes_read == 0 {
break;
}
println!("{}", line.trim());
line.clear();
}

Ok(())
}

In this example, we used a loop to read each line of the file. We created a new empty String called line and passed it to the read_line method, Which reads a line of text from the buffer and stores it in line. The method returns the number of bytes read, Which we are using to check whether we’ve reached the end of the file. If bytes_read is 0, It means we’ve reached the end of the file and we can break out of the loop. Otherwise, We will print the line of text and clear the String so, It can be used to store the next line.

Error Handling

When reading or writing files in Rust, It’s important to handle errors properly. Rust’s file operations return a Result type that can represent either success or failure. If an error occurs while reading or writing a file, The Result will contain an Err variant with an associated error message.

To handle errors when reading or writing files in Rust, We can use the match statement to pattern match on the Result. Here’s an example of error handling when reading a file line by line in Rust using match:

use std::fs::File;
use std::io::{BufRead, BufReader};

fn read_file_lines(filename: &str) -> Result<(), std::io::Error> {
let file = File::open(filename)?;
let reader = BufReader::new(file);

for line in reader.lines() {
match line {
Ok(line) => println!("{}", line),
Err(err) => return Err(err),
}
}

Ok(())
}

fn main() -> Result<(), std::io::Error> {
read_file_lines("example.txt")
}

In this example, we defined a function called read_file_lines that takes a filename as an argument and returns a Result<(), std::io::Error>. The function opens the file, Creates a BufReader, and reads the file line by line. We used match to pattern match on the Result returned by reader.lines(). If the result is Ok(line), We prints the line of text. If the result is Err(err), We propagate the error up the call stack by returning the error.

In the main function, we called read_file_lines with the filename “example.txt”. If an error occurs while reading the file, the error is returned by the main function and can be handled by the calling code.

Conclusion

In this article, we learned how to read and write files in Rust, including how to read a file line by line using a BufReader. We’ve also discussed error handling when reading or writing files in Rust, and provided code examples for each step along the way.

By following the examples and explanations provided in this article, you should now have a solid understanding of how to handle files in Rust. Whether you’re working with large data sets or simply reading and writing text files, Rust’s file handling capabilities make it a powerful tool for a wide range of programming tasks.

If you’re interested in learning more about programming and related topics, we invite you to check out our website programmingeeksclub.com. We offer valuable resources and insights.

You can find us on Twitter and Facebook.

Originally published at https://programmingeeksclub.com on April 23, 2023.

--

--