4 ms·
The strings actually make sense. The difference between String and &str is same as between Vecs and slices. Don't use OsStr unless you have to.
by adamnemecek 6y ago
The strings actually make sense. The difference between String and &str is same as between Vecs and slices. Don't use OsStr unless you have to.
- woodruffw 6y agoAuthor here: I agree that they make sense! I think the nuance is between "makes sense" and "fits into the mental model Rust otherwise encourages," i.e. thinking about `T` and `&T` as the fundamental building blocks for ownership semantics. In that context, being unable to directly instantiate a `str` (or having `&String` be a thing that you can occasionally produce when you mean `&str) is deeply confusing to newcomers.
- jkoudys 6y agoI'd even made your same comment about Strings over on reddit last week - it's not like the ways it's doing it are wrong at all. It's more that newbies can suffer decision paralysis when you are obsessed with learning the "right way" to do something, but there's no clear answer. Do I `.map(String::from)`, do I `.map(From::from)` and let things infer? Do I `.map(|s| s.into())`? `.map(|s| s.to_owned())`? `.map(|s| s.to_string())`? You can drive yourself crazy over the most meaningless, pointless questions in your code. The surprising thing is, I find rust an incredibly effective language at sparing me from worrying about trivialities. Once you learn the slight differences in what all those things mean, you can be pretty direct. Overall I find rust one of the harder languages to write, but one of the easiest to read, and reading code is generally where you'll spend more time.
- ajross 6y agoThe problem is naming though. Calling something a "slice" connotes the existence of a whole somewhere from which the slice was taken. The string types just call themselves same thing with some funny syntax and oddly inconsistent capitalization and abbreviation conventions. This non-behavioral stuff matters. Almost literally no one who comes to rust understands the string types with any intuition initially. And strings are really important! Much the same thing can be said about the evolution of other areas of rust syntax (macros and attributes come immediately to mind). The final result is a collection of syntactic soup with (mostly) well-defined and internally consistent behavior, but with a syntactic expression that bears the archaeological scars of its evolution.
- adamnemecek 6y ago> The problem is naming though. Calling something a "slice" connotes the existence of a whole somewhere from which the slice was taken. The string types just call themselves same thing with some funny syntax and oddly inconsistent capitalization and abbreviation conventions. That is not the case.
- twic 6y agoThis diagram may help: https://docs.google.com/presentation/d/1q-c7UAyrUlM-eZyTo1pd8SZ0qwA_wYxmPZVOQkoDmH4/edit https://docs.google.com/presentation/d/1q-c7UAyrUlM-eZyTo1pd... Although it could really do with some more expository text!