expression
Apply a file plan without overwriting existing output
For a report generator with a no-overwrite contract, attempt exclusive creation directly. A prior exists() check alone leaves a race. This fixture intentionally supplies an existing file and confirms its contents are unchanged. Creation does not make subsequent writes atomic. Fixture: scratch files {'report.txt': 'old\n'}. Expected stdout (not executed): existing report preserved Expected stderr: (empty) Expected exit: 0 Common mistake: Using File::create after checking existence; another process may create the file in between.
Formal Syntax & Structure
rust syntaxrust
OpenOptions::new().write(true).create_new(true).open(path)Working Example
Example Snippetrust
fn main() { use std::{fs::{self, OpenOptions}, io::ErrorKind}; let result = OpenOptions::new().write(true).create_new(true).open("report.txt"); assert_eq!(result.unwrap_err().kind(), ErrorKind::AlreadyExists); assert_eq!(fs::read_to_string("report.txt").unwrap(), "old\n"); println!("existing report preserved");}