coding-horror/basic-computer-games · error

ERROR WRITING Stats FILE!

Error message

ERROR WRITING Stats FILE!

What it means

This panic is triggered by `.expect("ERROR WRITING Stats FILE!")` on `write!(&mut file, ...)` inside `Stats::write()` in Splat (line 86). It fires when the `write!` macro fails to write the formatted altitude CSV string to the file handle. The file was just created by `File::create` on the preceding line, so a write failure here implies a low-level I/O problem after successful creation.

Source

Thrown at 81_Splat/rust/src/stats.rs:86

        utility::print_win(all_jumps, placement);

        self.altitudes.push(a);
        self.altitudes.sort_by(|a, b| a.partial_cmp(b).unwrap());

        self.write();
    }

    fn write(&self) {
        let mut file = File::create("src/stats.txt").expect("Error loading stats data!");

        let mut altitudes = String::new();

        for a in &self.altitudes {
            altitudes.push_str(format!("{},", a).as_str());
        }

        write!(&mut file, "{}", altitudes.trim()).expect("ERROR WRITING Stats FILE!");
    }
}

View on GitHub (pinned to 5301155192)

Solutions

  1. Replace `.expect` with a `match`/`if let` that logs the error and warns the user that stats were not saved.
  2. Write to a temporary file first and atomically rename on success, to avoid corrupting the existing stats file on partial write failure.
  3. Check available disk space before writing in environments where this is a concern.
  4. Handle `write!` error by retrying once before giving up.

Example fix

// before
write!(&mut file, "{}", altitudes.trim()).expect("ERROR WRITING Stats FILE!");

// after
if let Err(e) = write!(&mut file, "{}", altitudes.trim()) {
    eprintln!("Warning: could not write stats: {}", e);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Check available disk space before writing (Unix)
use std::os::unix::fs::MetadataExt;
if let Ok(meta) = std::fs::metadata("src/") {
    if meta.blocks() * meta.blksize() < 1024 {
        eprintln!("Warning: low disk space.");
    }
}

Type guard

// No type guard; write! returns io::Result.
// Guard via match/if let on the Result.

Try / catch

if let Err(e) = write!(&mut file, "{}", altitudes.trim()) {
    eprintln!("Warning: could not save stats: {}", e);
    return;
}

Prevention

When it happens

Trigger: Disk full, quota exceeded, NFS/network filesystem failure, or filesystem corruption between `File::create` and `write!`. The content being written is a comma-joined list of `f32` altitudes, so the data itself cannot cause a format error — only the I/O layer can.

Common situations: Disk full on the volume containing `src/`. Network filesystem timeout during the write. Filesystem quota exceeded. Running on a read-only filesystem where `File::create` appeared to succeed but writes fail (rare).

Related errors


AI-assisted analysis of coding-horror/basic-computer-games@5301155192 (2026-08-13). Data as JSON: /api/errors/1fdf62a4daea0360. Report an issue: GitHub.