expression

Optional configuration file: distinguish missing and unreadable

An optional config file can be absent, but other open errors should not become defaults. This example uses a fixed missing path in a fresh scratch directory. A user-specified required path should report even NotFound as an error. Fixture: Expected stdout (not executed): no optional config Expected stderr: (empty) Expected exit: 0 Common mistake: Ignoring every filesystem error and silently running with defaults.

Formal Syntax & Structure

rust syntaxrust
match File::open(path) { ... }

Working Example

Example Snippetrust
fn main() {    use std::{fs::File, io::ErrorKind};    match File::open("missing.toml") {        Ok(_) => panic!("fixture directory was not empty"),        Err(e) if e.kind() == ErrorKind::NotFound => println!("no optional config"),        Err(e) => panic!("could not open configuration: {:?}", e.kind()),    }}
Rust Reference