ErrorKind::NotFound
RustERRORCommonI/O

No such file or directory

Quick Answer

Check the path exists before opening, or create the file with OpenOptions.

What this means

The requested file or resource does not exist. Returned when an OS operation fails because the path does not exist.

Why it happens
  1. 1File path does not exist
  2. 2Typo in path string
  3. 3Working directory differs from expected

Fix

Match on ErrorKind

Match on ErrorKind
use std::fs;
use std::io::ErrorKind;

match fs::read_to_string("config.toml") {
    Ok(s) => println!("{}", s),
    Err(e) if e.kind() == ErrorKind::NotFound => eprintln!("File not found"),
    Err(e) => eprintln!("IO error: {}", e),
}

Why this works

e.kind() returns the ErrorKind variant, letting you handle specific IO cases.

Code examples
Triggerrust
let _ = std::fs::read("missing.txt"); // NotFound
Create if missingrust
use std::fs::OpenOptions;
let f = OpenOptions::new().create(true).write(true).open("f.txt")?;
Sources

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

← All Rust errors