Empty contents of file

This kind of a nooby question. What is the most efficient and/or idiomatic way to clear the contents of a file using the standard library?

If the file is already open you can use .set_len(0). Else you can use std::fs::OpenOptions::new().truncate(true).open(path) to open a file and truncate it at the same time.

3 Likes

If you give an existing file path to File::create, it will be truncated to 0 bytes.

2 Likes