A Guide to Working With Text Files in Rust

Rust’s high performance and memory safety, combined with the support for regular expressions and efficient file I/O operations, enable compelling text and data processing, making Rust an excellent language for working with text files.

Text files are a shared data source for applications, and Rust provides a robust set of tools for working with text files in the standard library.

Working With Text Files in Rust

Rust’s standard library provides several modules for reading, writing, and manipulating files, including text files.

Working with text files in Rust involves reading data from a file, using the data in memory, and then writing the modified data back to the file.

Rust provides the built-in fs module for working with files and the file system. The fs module provides functions and types that you can use to open, read, write, and manipulate files and directories, including text files.

Popular functions in the fs module include the fs::File::open function for opening files for read operations, the fs::File::create for creating files, and the fs::write function for writing data to files.

The fs module also provides functions like the fs::remove_file function for deleting files, the fs::create_dir function for creating directories, and the fs::read_dir function for reading files in a directory.

The fs module is versatile, and you can use the fs module to work with most file types, including text files.

Reading Text Files in Rust

You have to open a text file to read the file. You’ll use the fs::File::open function to open the file before the read operation.

Run these commands to create a text file and write a sentence to the file from your command line.

 touch input.txt

echo "We're tech enthusiasts on a mission to teach the world how to use and understand the tech in their lives. Phones, laptops, gadgets, apps, software, websites, services---if it can make your life better, we'll show you all the tips, tricks, and techniques you need to know to get the most out of what you have." > input.txt

Now that you have an existing text file with contents for the read operation, you can write Rust code to read the file’s contents.

Here’s how you can read from the input.txt file:

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

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

    let reader = BufReader::new(file);

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


The main function returns the result of the operation. The file variable is the open file instance that the new function of the BufRead module takes in to create a buffered reader to read the file.

The for loop control structure traverses through the file and prints each line from the file to the console with the println! macro.

result from reading the text-file-1

Writing to Text Files in Rust

You’ll need to create a new file with the fs::File::create function (if the file doesn’t exist) to write to a text file.

Here’s how you can create and write text files in Rust:

 use std::fs::File;
use std::io::{BufWriter, Write};

fn main() -> std::io::Result<()> {
    let file = File::create("output.txt")?;

    let mut writer = BufWriter::new(file);

    writer.write_all(b"Hello, world!n")?;
    writer.write_all(b"Rust is awesome.n")?;


    println!("Write Operation Successful");

The file variable is an instance of the created output.txt file, and the writer variable is a buffered writer instance for writing to the file.

The write_all function writes data to the file, and the flush function flushes the writer.

Here’s the result from running the main function:

result of writing to the text file

You Can Use Text Files for Several Operations

Text files are lightweight and handy for multiple purposes, from configuration files to data exchange, logging, and text processing. As you build applications in Rust or any other programming language like Go, you can explore other use cases for text files to ease your development workflows.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button