std-method

File::create: create or truncate

Open for writing, creating a missing file or truncating an existing file. Use this only when replacement is intentional. Run only inside a disposable working directory. The fixture must not already exist. Expected stdout (not executed): new! Common mistake: Calling create just to check if a file can be opened.

Formal Syntax & Structure

rust syntaxrust
File::create(path) -> io::Result<File>

Working Example

Example Snippetrust
fn main() {    use std::fs::{self, File, OpenOptions};    use std::io::{Read, Write};    fs::create_dir("bm-fixture").expect("use a fresh disposable directory");    let p = std::path::Path::new("bm-fixture").join("note.txt");    File::create_new(&p).unwrap().write_all(b"old").unwrap();    File::create(&p).unwrap().write_all(b"new!").unwrap();    println!("{}", fs::read_to_string(&p).unwrap());}
Rust Reference