Working with the File System in Rust: A Complete Guide for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Working with the File System in Rust

Rust is a modern systems programming language that places a strong emphasis on safety, performance, and memory control. Working with the file system is one of the fundamental tasks every developer encounters. In this article, we will thoroughly examine how to read and write files, create directories, handle errors, and use the standard library std::fs for efficient interaction with the file system.

We will start with the very basics and gradually move on to more complex scenarios. All code examples are written in Rust and are ready to run. If you are just starting to learn Rust, this article will be an excellent starting point for mastering input/output (I/O) in this language.



Core Modules and Types for Working with the File System

In Rust, all functionality for working with files and directories is concentrated in the std::fs module. Additionally, the Path and PathBuf types from the std::path module play an important role, representing paths to files and directories.



Path and PathBuf Types

Path is a slice for working with paths without owning the data. PathBuf is an owned version that can be modified. They provide convenient methods for path manipulation.

use std::path::Path;use std::path::PathBuf;

fn main() { // Creating a PathBuf let mut path = PathBuf::from("/tmp"); path.push("example.txt"); println!("Path: {}", path.display());

// Converting to Path let path_ref: &Path = path.as_path(); println!("Parent directory: {:?}", path_ref.parent());}


The std::fs Module

The std::fs module contains the core functions for working with the file system. Here are the most commonly used ones:

  • read_to_string — reading a file into a string
  • write — writing data to a file
  • create_dir and create_dir_all — creating directories
  • remove_file and remove_dir — deleting files and directories
  • metadata — getting file information
  • copy and rename — copying and renaming


Reading and Writing Files in Rust

The simplest way to read or write a file is to use wrapper functions that do all the work in a single call. They are suitable for small files that fit entirely in memory.



Reading a File Entirely

use std::fs;

fn main() -> Result<(), std::io::Error> { let content = fs::read_to_string("hello.txt")?; println!("File contents:{}", content); Ok(())}

The read_to_string function returns a Result<String>. If the file does not exist or there are no read permissions, an error will be returned. Note the ? operator — it automatically propagates the error upward.



Writing to a File

use std::fs;

fn main() -> Result<(), std::io::Error> { let data = "Hello, Rust!"; fs::write("output.txt", data)?; println!("File written successfully"); Ok(())}

The write function overwrites the file if it exists, or creates a new one. To append data to the end of a file, use OpenOptions.



Working with Binary Files

For reading and writing binary data, use read and write with a vector of bytes:

use std::fs;

fn main() -> Result<(), std::io::Error> { // Writing binary data let bytes: Vec<u8> = vec![0, 1, 2, 3, 255]; fs::write("binary.dat", &bytes)?;

// Reading as bytes let read_bytes = fs::read("binary.dat")?; println!("Read

Blogs

Book Recommendations