Loading posts...
Loading posts...
Rust, a language celebrated for its performance and safety, also provides powerful and straightforward tools for file I/O operations. Let's take a closer look at a simple Rust program that reads, parses, and writes data from and to text files. This example illustrates Rust's capabilities for handling file-based data, a common task in many applications.
Our focus is a Rust program designed to read a text file containing names and dates of birth (DOB), process the data, and write the formatted output to another file. The simplicity of the task underlines Rust's efficiency in managing file operations.
The program starts by reading a file named DOB.txt
:
let contents = fs::read_to_string("./DOB.txt").expect("File path does not exist");
This line utilizes Rust's std::fs
module to read the entire file into a string. The expect
method is used here to handle any potential error that might occur while reading the file, such as if the file does not exist.
The next step is to parse the contents of the file:
let vec_contents: Vec<Vec<_>> = contents
.lines()
.map(|line| line.split_whitespace().collect())
.collect();
Here, the program processes each line of the file, splitting it into words based on whitespace. The result is a vector of vectors, where each inner vector represents the words of a line.
After parsing, the program formats and prints the data:
println!("Name | DOB\n=========================");
let mut data = String::from("Name | DOB\n=========================");
vec_contents.iter().for_each(|p| {
println!("{} | {}", p[0..2].join(" "), p[p.len() - 3..].join(" "));
data.push_str(&format!(
"\n{} | {}",
p[0..2].join(" "),
p[p.len() - 3..].join(" ")
))
});
The program assumes the first two elements of each line are the name and the last three elements are the date of birth. It joins these elements with a vertical bar (|
) and prints them. This format is also appended to a String
variable data
for writing to the output file.
Finally, the formatted data is written to an output file:
let _ = fs::write("./output.txt", data);
The fs::write
function is used to create (or overwrite if it already exists) an output file named output.txt
, containing the formatted data.
expect
ensures that the program handles potential errors, such as missing files, gracefully.map
and collect
, offer a concise and efficient way to process and transform data.This Rust program exemplifies the language's capabilities in handling common tasks like file reading, data parsing, and writing to files. The combination of Rust's safety features, efficient data handling, and powerful standard library makes it an excellent choice for file processing tasks. Whether you're new to Rust or looking to brush up on file I/O operations, this example serves as a practical guide to Rust's approach to handling file-based data.