I’m trying to list all the files in a directory. So I looked up std::fs::read_dir, and it says:
This function will return an error if the provided path doesn’t exist, if the process lacks permissions to view the contents or if the path points at a non-directory file.
Now I can destructure this into Ok() and Err(), but how do I know what concrete Err() I got thrown so I can handle it? The documentation doesn’t have any list, nor does the type suggest anything.
Alright, maybe it’s in the source. So I try to look up std::fs::read_dir, and see it’s actually implemented in fs_imp::readdir (note the change in spelling), which is an alias for sys::fs, which I can’t find in the online docs. Fine, grepping the source it is. So readdir() differs by os (do its errors? doesn’t quite look like it, but how can I tell without reading everything?). Anyway, so it throws Error::last_os_error(). That’s of course not in error.rs, but Error here is io::Error, and there I finally find out that it returns the OS’s error number.
So to handle the error, I’ll have to look up all error numbers any OS I want to support can throw, and check them, with (if I’m not mistaken) no help from match to show I conclusively caught all of them.
Is that right?
More importantly, is this usage of
Result<T, E> normal, where the error is extremely vague and basically just means “stuff blew up, but at least your thread hasn’t crashed”? Wouldn’t you want to return precise error enums?
(And is this situation with std::fs typical, where I have to dig through several source files to figure out what it does? Or is std::fs just really barebones and under-documented? If so, what else should I use?)