|
|
|
|
|
by ComputerGuru
3026 days ago
|
|
Well, that's actually the root problem which I avoided discussing until now: why does String have a str special case but no other data type does? Same with Path and PathBuf. Why does there need to be a distinct data type for a non-owned view into an object? Why is that not just part of the language in the first place? C++ needs string vs string_view because it has no borrow checker, but rust could (theoretically) implement this without the need for two different types. |
|
Vec's "special case" is [].
And it's not that String "has" a special case, it's that str is a special fundamental case of the language in the same way i8 or () is and String exists to make it easier to work with (otherwise you'd have to deal with Box<str> and efficiently working with that would require going back and forth to Vec, except you'd have removed Vec since it'd be a special case of [] and where do you store capacities at this point?)
> C++ needs string vs string_view because it has no borrow checker, but rust could (theoretically) implement this without the need for two different types.
C++ has char* which conceptually underlies both string and string_view. Rust shoves char* in the unsafe corner but need something to replace it, something which not only means "a bag of bytes" ([u8] does that just fine) but text as in "actual proper utf8-encoded unicode text". That's str.