If you know you want the directory to exist, and you want to create it if it doesn't, you can just unconditionally call std::fs::create_dir_all on the directory.
Are you looking for the logic of "create file, if it fails with ENOENT then create_dir_all and try again"? That would save one syscall in the case where the directory exists, and cost one syscall in the case where the directory doesn't exist. In some cases that may be the right performance tradeoff.
I don't know if it makes sense to make that part of OpenOptions
, though. OpenOptions
seems consistently about opening/creating a file, and doesn't fold in other associated operations. I think it would make more sense for this to be a separate function.