Home »
Rust »
Rust Programs
Rust program to create an empty file
Rust | File I/O Example: Write a program to create an empty file.
Submitted by Nidhi, on October 30, 2021
Problem Solution:
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.
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.
// 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.
Rust File I/O Programs »