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
- 1fs::create_dir on an existing directory
- 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")?; // AlreadyExistsSame error in other languages
Sources
Official documentation ↗
Rust std::io
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev