In this program, we will create an empty file using File::create() function and print an appropriate message on the console screen based on the successful creation of the file.
The source code to create an empty file is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.
// Rust program to create an empty file
fn main() {
std::fs::File::create("empty.txt").expect("create failed");
println!("File created successfully");
}
Output:
$ rustc main.rs
$ ./main
File created successfully
$ ls
empty.txt main main.rs
Explanation:
Here, we created an empty file "empty.txt" using File::create() function. If file creation failed due to any permission issue, then "create failed" will be printed. Otherwise "File created successfully" message will be printed.
Program/Source Code:
The source code to create an empty file is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.
Output:
Explanation:
Here, we created an empty file "empty.txt" using File::create() function. If file creation failed due to any permission issue, then "create failed" will be printed. Otherwise "File created successfully" message will be printed.
need an explanation for this answer? contact us directly to get an explanation for this answer