w3resource

Rust File Operations: Copy, Move, Delete with Error handling

Rust Error Propagation: Exercise-2 with Solution

Write a Rust program that performs multiple file operations (e.g., copy, move, delete) and handles errors at each step.

Sample Solution:

Rust Code:

use std::fs; // Import the fs module for file handling
use std::io; // Import the io module for file I/O operations

// Function to copy a file from source to destination
fn copy_file(source: &str, destination: &str) -> Result<(), io::Error> {
    fs::copy(source, destination)?; // Copy the file from source to destination
    Ok(()) // Return Ok if the operation completes successfully
}

// Function to move (rename) a file from source to destination
fn move_file(source: &str, destination: &str) -> Result<(), io::Error> {
    fs::rename(source, destination)?; // Move the file from source to destination
    Ok(()) // Return Ok if the operation completes successfully
}

// Function to delete a file
fn delete_file(file_path: &str) -> Result<(), io::Error> {
    fs::remove_file(file_path)?; // Delete the file at the specified path
    Ok(()) // Return Ok if the operation completes successfully
}

fn main() -> Result<(), io::Error> {
    let source_file = "source_file.txt"; // Specify the path of the source file
    let destination_file = "destination_file.txt"; // Specify the path of the destination file

    fs::write(source_file, "Sample text")?; // Create the source file with sample content

    println!("Copying file from {} to {}...", source_file, destination_file);
    copy_file(source_file, destination_file)?; // Copy the source file to the destination

    println!("Moving file from {} to {}...", source_file, destination_file);
    move_file(source_file, destination_file)?; // Move the source file to the destination

    println!("Deleting file {}...", destination_file);
    delete_file(destination_file)?; // Delete the destination file

    println!("File operations completed successfully.");

    Ok(()) // Return Ok if the program completes successfully
}

Output:

Copying file from source_file.txt to destination_file.txt...
Moving file from source_file.txt to destination_file.txt...
Deleting file destination_file.txt...
File operations completed successfully.

Explanation:

Here is a brief explanation of the above Rust code:

  • Importing Modules:
    • use std::fs: Imports the "fs" module from the standard library for file handling.
    • use std::io: Imports the "io" module from the standard library for handling I/O operations.
  • Function Definitions:
    • copy_file: Function to copy a file from a source path to a destination path. It uses fs::copy to copy.
    • move_file: Function to move (rename) a file from a source path to a destination path. It uses fs::rename to move.
    • delete_file: Function to delete a file at the specified path. It uses fs::remove_file to delete the file.
  • Main Function (main()):
    • It starts by defining the paths of the source file (source_file) and the destination file ('destination_file').
    • The source file is created with sample content using fs::write.
    • It then calls the "copy_file()" function to copy the source file to the destination file, printing a message indicating the operation.
    • Next, it calls the "move_file()" function to move (rename) the source file to the destination file, printing a message indicating the operation.
    • Finally, it calls the "delete_file()" function to delete the destination file, printing a message indicating the operation.
    • If any of the file operations encounter an error, it propagates the error up to the main function, which returns an error result.
    • If all operations complete successfully, it prints a message indicating that the file operations were completed successfully and returns an Ok result.

Rust Code Editor:

Previous: Rust File Processing Function: Read, Process, and Write with Error Handling.
Next: Rust Program: Interacting with Web Service for Authentication & Data retrieval.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/rust/error_handling/rust-error-propagation-exercise-2.php