Rust program to remove an existing file

Rust | File I/O Example: Write a program to remove an existing file.
Submitted by Nidhi, on October 30, 2021

Problem Solution:

In this program, we will remove a specified file using the remove_file() function and print the appropriate message.

Program/Source Code:

The source code to remove an existing file is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to remove an existing file

use std::fs;

fn main() {
   fs::remove_file("sample.txt").expect("Unable to remove file");
   println!("File samplet.txt is removed successfully");
}

Output:

$ rustc main.rs

$ ./main
File samplet.txt is removed successfully

Explanation:

Here, we removed the specified file "sample.txt" and printed the appropriate message.

Rust File I/O Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.