I wrote some code very similar to this recently and was surprised to learn that it always fails the first time you run it. See if you can spot the reason why.

``` use std::error::Error; use std::fs::File; use std::io::Read; use std::path::Path;

fn main() -> Result<(), Box<dyn Error>> { let path = Path::new("hello.txt");

// Open file at path, creating it it does not yet exist.
let mut file = match File::open(&path) {
    Ok(file) => {
        eprintln!("Using file at {}", &path.to_string_lossy());
        file
    }
    Err(_) => {
        eprintln!("Creating new file {}", &path.to_string_lossy());
        File::create(&path)?
    }
};

let mut contents = String::new();
file.read_to_string(&mut contents)?;

Ok(())

} ```

Answer: File::create opens the file in write-only mode. I should have been calling File::create_new instead. Once I found that File::create exists I didn't keep reading. The _new suffix doesn't really convey the difference between these two functions. Doesn't "create" imply "new?"

Should I have declared let mut file = in a way that specified both read and write? Can the type system even express the difference between different open modes?