4 ms·
Rust the language has one string type: &str The standard library also has String, CString, CStr, OsString, and OsStr. The latter four are for niche situations
by steveklabnik 1y ago
Rust the language has one string type: &str
The standard library also has String, CString, CStr, OsString, and OsStr.
The latter four are for niche situations. 99.9% of the time, it's similar to Java: &str is Java's String, String is Java's StringBuffer/StringBuilder.
- ameliaquining 1y agoI would add Path and PathBuf to that list.
- steveklabnik 1y agoPaths are not strings, that's the whole point!
- simonask 1y agoWell... They're also not _not_ strings. In fact, what a perfect candidate for introducing a special type. :-)
- shikon7 1y agoThere is also &mut str. The nice thing about Rust is that the same memory area can be used as a "StringBuilder" with &mut str, and then as a "String" with &str. Once you have a &str reference, Rust statically guarantees that no one else cam mutate it.
- steveklabnik 1y agoYeah the type is really just str but I didn’t want to get into it. &mut str is kinda weird.
- simonask 1y agoYou can't really do anything useful with a `&mut str`. For example, you can't change the length of the string or any characters in the string (without unsafe code). The latter is perhaps surprising, but it's because strings in Rust are always UTF-8, so changing a logical character could imply changing the length of the string, and changing a byte could result in invalid UTF-8.
- whytevuhuni 1y agoYou're right, but just to add a bit more to this, you can call make_ascii_lowercase and make_ascii_uppercase on &mut str, which can sometimes be useful.
- fud101 1y ago99.9% of the time rust has 1 string type. The other 0.1% of the time it has 99, got it.
- steveklabnik 1y agoIt has two, not 99, with owned and borrowed pairs.