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 »



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.