3 ms·
> but you rarely need the latter AFAIK, it's because people go with String when what they actually mean is Box<str>. Since they have similar costs, nobody ever
by marcosdumay 4y ago
> but you rarely need the latter
AFAIK, it's because people go with String when what they actually mean is Box<str>. Since they have similar costs, nobody ever sees the need to change it, and the String type does have a much better name.
But the need is there all the time. People just satisfy it differently.
- sirwhinesalot 4y agoI think it's mainly because unlike Java, where a StringBuilder is effectively an optimisation over concatenating Strings, in Rust managing that memory would be a total pain, so you tend to keep the mutable thing around. Once that happens, Box<str> becomes kinda unnecessary. There are many cases where it would be the correct type, for example reading from a file in a read-only manner, but most of the time you're going to be doing something to that text, so it makes more sense to just load it up as a String already and avoid the unnecessary copy. Either way, it's mostly a naming problem. &str/String sucks :(
- afdbcreid 4y agoIt's not just the naming: I prefer String if I don't care about the data structure size, since converting a String to Box<str> can be costly (allocation and copy if the capacity isn't exact) and carrying a capacity is free pretty much all of the time.