Skip to main content
St Louis

Back to all posts

How to Read And Write Files In Rust?

Published on
3 min read
How to Read And Write Files In Rust? image

Best File I/O Guides for Rust Programming to Buy in September 2025

1 The Rust Programming Language, 2nd Edition

The Rust Programming Language, 2nd Edition

BUY & SAVE
$30.13 $49.99
Save 40%
The Rust Programming Language, 2nd Edition
2 Programming Rust: Fast, Safe Systems Development

Programming Rust: Fast, Safe Systems Development

BUY & SAVE
$43.99 $79.99
Save 45%
Programming Rust: Fast, Safe Systems Development
3 Rust for Rustaceans: Idiomatic Programming for Experienced Developers

Rust for Rustaceans: Idiomatic Programming for Experienced Developers

BUY & SAVE
$29.99 $49.99
Save 40%
Rust for Rustaceans: Idiomatic Programming for Experienced Developers
4 Rust in Action

Rust in Action

BUY & SAVE
$51.42 $59.99
Save 14%
Rust in Action
5 Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

BUY & SAVE
$47.04 $59.95
Save 22%
Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)
6 Zero To Production In Rust: An introduction to backend development

Zero To Production In Rust: An introduction to backend development

BUY & SAVE
$49.99
Zero To Production In Rust: An introduction to backend development
7 The Rust Programming Language

The Rust Programming Language

BUY & SAVE
$16.92 $39.95
Save 58%
The Rust Programming Language
8 Rust Atomics and Locks: Low-Level Concurrency in Practice

Rust Atomics and Locks: Low-Level Concurrency in Practice

BUY & SAVE
$33.13 $55.99
Save 41%
Rust Atomics and Locks: Low-Level Concurrency in Practice
9 Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes

Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes

BUY & SAVE
$28.90 $49.99
Save 42%
Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes
+
ONE MORE?

In Rust, you can read and write files using the std::fs module. To read a file, you can use the std::fs::File struct along with the std::io::Read trait. You can open a file using the File::open method and then read its contents using the Read::read_to_string method.

To write to a file, you can use the std::fs::File struct along with the std::io::Write trait. You can create a file using the File::create method and then write data to it using the Write::write_all method.

Remember to handle errors when reading or writing files by using the Result type and checking for Ok or Err values. Additionally, you should use the std::io::Error type for error handling.

Overall, reading and writing files in Rust involves using the standard library's file I/O functionalities and handling errors appropriately.

How to write data to a file in Rust?

To write data to a file in Rust, you can use the std::fs::File module. Here is an example code snippet that demonstrates writing data to a file in Rust:

use std::fs::File; use std::io::prelude::*;

fn main() { let mut file = File::create("output.txt").expect("Unable to create file");

let data = "Hello, world!";
file.write\_all(data.as\_bytes()).expect("Unable to write data to file");

}

In this code snippet, we first create a new file called "output.txt" using the File::create() method. We then write the string "Hello, world!" to the file using the write_all() method.

Don't forget to handle any errors that may occur when creating or writing to the file by using the expect() method or other error handling techniques.

How to create a new directory in Rust?

To create a new directory in Rust, you can use the std::fs module from the standard library. Here's an example of how you can create a new directory:

use std::fs;

fn main() { // Specify the path of the new directory let path = "new_directory";

// Create the new directory
match fs::create\_dir(path) {
    Ok(\_) => println!("Successfully created new directory"),
    Err(e) => eprintln!("Error creating directory: {}", e),
}

}

In this example, the create_dir function is used to create a new directory with the specified path. If the directory is successfully created, a success message is printed to the console. If there is an error, an error message is printed instead. You can modify the path variable to specify the location and name of the new directory you want to create.

How to open a file in Rust?

To open a file in Rust, you first need to use the std::fs::File module from the standard library. Here's an example code snippet that shows how to open a file in read-only mode:

use std::fs::File; use std::io::prelude::*;

fn main() { // Open the file in read-only mode let file = File::open("example.txt");

match file {
    Ok(mut f) => {
        // Read the contents of the file
        let mut contents = String::new();
        f.read\_to\_string(&mut contents).expect("Error reading the file");
        println!("{}", contents);
    },
    Err(e) => {
        // Handle the error if file cannot be opened
        eprintln!("{}", e);
        return;
    }
}

}

In the example above, we use File::open("example.txt") to open a file named "example.txt" in read-only mode. We then read the contents of the file using f.read_to_string(&mut contents) and print it out. Alternatively, you can also open a file in write or append mode by using File::create("example.txt") or File::open("example.txt").append(), respectively.