Re:Small Strings in Rust: smolstr vs. smartstring
by tialaramex on Saturday August 24, 2024 @06:37PM
(#41342338)
Thanks! And the explanation of 217 makes sense too.
Since I have you here, wouldn't it be better to name that type "LastByte" or something? It's not a (Rust) char, and it's not necessarily UTF-8 whereas it is definitely the last byte.
Re:Small Strings in Rust: smolstr vs. smartstring
by parkmycar on Saturday August 24, 2024 @06:56PM
(#41342507)
Ha, naming is hard! You’re totally right, it used to be just the values of the last byte of a UTF-8 char (and before that it was NonMaxU8) but now represents more. I’ll update it once I’m back at my computer, thanks!
Re:Small Strings in Rust: smolstr vs. smartstring
by tialaramex on Saturday August 24, 2024 @07:33PM
(#41342823)
Ah, while you're in there, IIRC you mention NonMaxU8 in the code still and probably that's worth updating at the same time.
> ... and another (217) signifies that it's a weird static allocation (I do not know why you'd do this)
In addition to String Rust also has str[1], which is an entirely different type. It's common to represent string literals known at compile time as `&'static str`, but they can't be used in all of the same places that a String can. For example, you can't put a &'static str into a Vec<String> unless you first heap allocate and create a String. We added the additional variant of 217 so users of CompactString could abstract over both string literals and strings created at runtime to solve cases like the example.
[1]: https://doc.rust-lang.org/std/primitive.str.html