ErrorKind::AlreadyExists
RustERRORCommonI/O

Entity already exists

Quick Answer

Use OpenOptions with create(true).truncate(true) to overwrite, or check existence first.

What this means

A create-exclusive operation failed because the target file or resource already exists.

Why it happens
  1. 1fs::create_dir on an existing directory
  2. 2OpenOptions with create_new(true) on existing file

Fix

Create only if absent

Create only if absent
use std::io::ErrorKind;
match std::fs::create_dir("output") {
    Ok(_) => {}
    Err(e) if e.kind() == ErrorKind::AlreadyExists => {}
    Err(e) => return Err(e.into()),
}

Why this works

Treats AlreadyExists as a non-error, idempotent creation.

Code examples
Triggerrust
std::fs::create_dir("existing_dir")?; // AlreadyExists
Same error in other languages
Sources

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Rust errors