3 ms·
Not the OP, but off the top of my head, we have: * str and String, which are the ones we just call “strings” * CStr and CString, for interop with libraries th
by codeflo 5y ago
Not the OP, but off the top of my head, we have:
* str and String, which are the ones we just call “strings”
* CStr and CString, for interop with libraries that work with null-terminated strings
* OsStr and OsString, for interop with system libraries that expect non-UTF-8 encodings
* Path and PathBuf, which are also just strings
* [u8] and Vec<u8>, which are encoding-neutral binary strings (&[u8] is the type of b"Hello world") and are useful for interop with e.g. C++’s std::string
That’s 10.
(For non-Rust people who are curious: In these pairs, the first one is the type of a view of a string that’s stored elsewhere, and the second is the type that actually owns the string data. All of these 10 have their uses, but outside of interfacing with non-Rust libraries, you mostly work with str/String and Path/PathBuf.)
- pdimitar 5y agoOK, technically correct but I still disagree they are actually 10 because aside from `str` (which should be used only for constants really) and `String` everything else is strictly optional and you're not mandated to work with it unless you got a specific scenario (like CStr / CString). Compare this to Haskell where you have lazy and non-lazy / Unicode and non-Unicode strings which is at least 4 (I think there was more but I forgot now) and you are practically forced into using any of them depending on which library you're interacting with -- and I don't mean with C libraries in this instance. I personally never even used anything beyond `str` and `String` for any app development, including a bit lower level stuff (though not embedded or tight integration with system libraries).
- samus 5y agoHaskell also has `String`, which is a linked lazy list of boxed UTF32 characters. Yes, it's as horrible for performance as it sounds. Also, there are `ByteString`s which wrap strings in other encodings.
- codeflo 5y agoI don’t think we actually disagree, see my last paragraph.
- pdimitar 5y agoTrue, I overreacted a little, sorry. Thanks for being a good sport.