File Input & Output
File I/O (input/output) is an important part of many applications, and Rust provides a set of standard library functions for working with files.
Here are some examples of how to create, write, append, read, and delete files in Rust:
Creating a file
To create a file in Rust, you can use the File::create
function. This function takes a Path
argument that specifies the file to create, and returns a Result<File>
that contains a File
handle to the newly created file.
use std::fs::File; fn main() -> std::io::Result<()> { let file = File::create("example.txt")?; Ok(()) }
In this example, the File::create
function is used to create a new file named "example.txt". The ?
operator is used to handle any errors that may occur during the file creation process.
Writing to a file
To write data to a file in Rust, you can use the File::write_all
function. This function takes a byte slice as its argument and writes the contents of the slice to the file.
use std::fs::File; use std::io::prelude::*; fn main() -> std::io::Result<()> { let mut file = File::create("example.txt")?; file.write_all(b"Hello, world!")?; Ok(()) }
In this example, the File::write_all
function is used to write the string "Hello, world!" to the file. The b
prefix is used to convert the string to a byte slice.
Appending to a file
To append data to a file in Rust, you can use the File::open
function to open the file in append mode, and then use the File::write_all
function to write data to the end of the file.
use std::fs::OpenOptions; use std::io::prelude::*; fn main() -> std::io::Result<()> { let mut file = OpenOptions::new().append(true).open("example.txt")?; file.write_all(b"More data")?; Ok(()) }
In this example, the OpenOptions::new().append(true)
function is used to open the file in append mode, and the File::write_all
function is used to write the string "More data" to the end of the file.
Reading from a file
To read data from a file in Rust, you can use the File::read_to_string
function to read the entire contents of the file into a string.
use std::fs::File; use std::io::prelude::*; fn main() -> std::io::Result<()> { let mut file = File::open("example.txt")?; let mut contents = String::new(); file.read_to_string(&mut contents)?; println!("{}", contents); Ok(()) }
In this example, the File::open
function is used to open the file, the String::new
function is used to create a new empty string, and the File::read_to_string
function is used to read the entire contents of the file into the string. The contents of the string are then printed to the console.
Deleting a file
To delete a file in Rust, you can use the std::fs::remove_file
function. This function takes a Path
argument that specifies the file to delete.
use std::fs; fn main() -> std::io::Result<()> { fs::remove_file("example.txt")?; Ok(()) }
In this example, the fs::remove_file
function is used to delete the "example.txt" file.
Overall, Rust provides a set of standard library functions for working with files that make it easy to perform common file I/O operations such as creating, writing, appending, reading, and deleting files. By using these functions, Rust applications can efficiently and safely work with files, without the need for external libraries or manual memory management.