Skip to main content

Command Palette

Search for a command to run...

How to read files using Rust

I will be sharing how you can read files from your machine using Rust.

Updated
How to read files using Rust
A

Fullstack developer Webthreeing.

Hey Rustaceans

I will be sharing the fastest way to read files from your machine using Rust.

Do not worry every piece of code will be explained.

This guide is based on assumptions that you have rust installed.

Let's enjoy the ride.

First things first

Open your terminal and type the following command in your cli.

cargo new file-reader

then,

cd file-reader

your file structure should look like this

file-reader -

|-src|-main.rs

|-Cargo.toml

|- file.txt

Open main.rs and import the following

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

Here are what the two imports do.

Line 1 will import File which is a reference for opening files in your filesystem

Line 2 will expose some api that will help you read the opened file

With this in mind lets move ahead

Reading the file

Do not forget to move the file you want to read into your project root directory "file-reader"

In our case we are moving a file called data.txt and here's is a sample of my data.csv when opened in the text editor.

The quick 
brown fox
jumps over
a lazy dog.

create a new function called "file_handler" and insert the following code

fn main(){
...
}

fn file_handler() {
    let mut file = File::open("data.txt").expect("Cannot open file");
    let mut content = String::new();
    file.read_to_string(&mut content)
        .expect("Opps! cannot read file");
    println!("{}",&content);
}

Hmm! what have we done?

We initialized file as been mutable, we then opened data.txt by passing it into the open method. What happens if data.txt does not exist? The program may break and terminate unexpectedly. So to handle that we pass .expect() which handles any error while opening the file.

Phew! i will like you to know you have gone this far, but right now we can't do anything with the opened file, so we need to pass the data into a new empty string.

We initialized a new string called content, and then read the file into the content as string.

Note that Rust is low-level and we ,may need to handle any error they may arise when reading the file which may lead to an error if it is read protected. So to handle that we pass another expect method and then print the content of string.

lets call the file_handler function in our main function

fn main(){
file_handler();
}

fn file_handler() {
    let mut file = File::open("data.txt").expect("Cannot open file");
    let mut content = String::new();
    file.read_to_string(&mut content)
        .expect("Opps! cannot read file");
    println!("{}",&content);
}

Now we are done. Let's run our code and see what we have created.

cargo run

When i run my code, here's is the output containing the content of my data.txt file.

Finished dev [unoptimized + debuginfo] target(s) in 0.93s
     Running `target/debug/hello`
The quick 
brown fox
jumps over
a lazy dog.

Congratulations, you just read a file from your filesystem.

Turn on notifications to be among the first Rustaceans seeing my next post.

You can also find me on twitter .