std-method
OpenOptions::truncate: explicit replacement
Request truncation at open time with write access. If preserving old data matters, use a separate replacement design instead. Run only inside a disposable working directory. The fixture must not already exist. Expected stdout (not executed): 0 Common mistake: Truncating before validating replacement data.
Formal Syntax & Structure
rust syntaxrust
OpenOptions::new().write(true).truncate(true).open(path)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(); OpenOptions::new().write(true).truncate(true).open(&p).unwrap(); println!("{}", fs::metadata(&p).unwrap().len());}