expression

Dry-run: plan a change without applying it

Separate a side-effect-free plan from the execution branch. This scratch-file example uses a fixed destination and previews creation without opening it. A dry run may not predict future permission or race failures; the real action must recheck preconditions. Fixture: Expected stdout (not executed): would create report.txt (8 bytes) Expected stderr: (empty) Expected exit: 0 Common mistake: Opening a destination with truncate or create before checking dry-run. A preview is not a guarantee of later success.

Formal Syntax & Structure

rust syntaxrust
if dry_run { preview(&plan) } else { apply(&plan) }

Working Example

Example Snippetrust
fn main() -> std::io::Result<()> {    use std::{path::Path, fs::OpenOptions, io::Write};    let path = Path::new("report.txt");    let dry_run = true;    let content = b"summary\n";    if dry_run {        println!("would create report.txt ({} bytes)", content.len());    } else {        let mut file = OpenOptions::new().write(true).create_new(true).open(path)?;        file.write_all(content)?;    }    assert!(!path.exists());    Ok(())}
Rust Reference