As I started to use Rust more often (mainly because I think it’s an interesting language and I wanted to get more used to it), I started to encounter some things I found at first glance to be a little strange, like the relatively many string representations Rust has:
The first two are pretty easy to understand (both usage and raison d’etre):
The following are the ones where the story gets more convoluted:
- and their borrowed representation e.g: OsStr, CStr…etc
So it seems like the last list is comprised of specialized types for special use cases, but I started to encounter these types in some very simple cases like getting the filename of a Path:
// I just want a String ! let name = String::from(path.file_stem().unwrap().to_str().unwrap());
It seems like many functions from Path make use of OsStr instead of &str or String, making it quite cumbersome to work with, at least with my current expertise in Rust, which to be frank is quite limited !
How could I refactor some code like the one in the example above to make it more readable and have some proper error handling, and why is it necessary to have all those different types ?