I wonder why Rust doesn’t expose methods for getting the
str directly, but there sure is a reason for that.
Well, whereever there can go something wrong, there needs to be an
In this case, the
.file_name() of course must not simply return the
str, as at could be that there is
None to return.
Also the first conversion yields a one of these types, because the OsStr might not be convertible to valid UTF-8.
unwrap()s were not needed, something could go wrong, which, by design, should not be possible in Rust.
The explicit unwrapping of objects is something that could be automated, but isn’t for your convenience.
Just think of a random NullPointerException in Java or a Segmentation fault in C. These things are hard to find.
Rust makes it easy for you to not even have those problems, because if you write
unwrap() you know, there should have been some sort of error handling.