10 ms·
This is FAR too long. The first 10 pages is all about C strings. Just get to the point of why Rust has these 2 types and give people a link to a piece on unicod
by phkahler 4y ago
This is FAR too long. The first 10 pages is all about C strings. Just get to the point of why Rust has these 2 types and give people a link to a piece on unicode and UTF8 if that background is going to help. I didn't even get to the Rust part before giving up. I then scanned it, found a little Rust stuff but still couldn't easily pick out the answer to the top-level question. Now I don't care.
- ReactiveJelly 4y agoRust has multiple string types _because_ of the legacy of C strings.
- davidatbu 4y agoCan you explain how Rust's string types would be different if there was no "legacy of C strings" to deal with?
- tialaramex 4y agoFor anyone who does care, the answer is that String is not only mutable it's always heap allocated and can grow, whereas &str is just an immutable reference and so it could be pointing at the text in your String, but equally it could point to the stack, or your text segment, or a ROM. C++ programmers might do OK to think of &str as like a C++ 17 string_view, except it's promised to always be UTF-8 text, and because it was provided from the outset in the language this type is always used where appropriate, so the string literal "Test" is a &'static str, and all the APIs you use work with &str and never some archaic pointer type. Rust's String is a bit like C++ std::string, though deliberately lacking the Small String Optimisation but again always UTF-8, and String implements the Trait Deref<Target=str> which means most read-only methods are defined on &str but work fine on String anyway. [edited: A handful of read-only methods make sense specifically for String, like asking how much space is allocated for the String, so those are in fact methods on String]