7 ms·
It could slice on bytes and return a slice of bytes since the String type is a wrapper over Vec<u8>.
by throw0u1t 8y ago
It could slice on bytes and return a slice of bytes since the String type is a wrapper over Vec<u8>.
- dbaupp 8y agoThat means one loses all the conveniences and guarantees of the string types and, in many cases, forces an immediate revalidation the byte slice as UTF-8 to get back to &str, which is O(n). Furthermore, this is also rather clunky. I suppose one could have it return StrWithInvalidSurrounds, where just the first (at most) 3 and last (at most) 3 bytes might be invalid, which would then allow for O(1) revalidation to a &str, and even other operations like continuing to slice... But this is even more clunky for actual use! I think a moderately less clunky API might have been to not use integers for byte indexing, but instead some ByteIndex wrapper type that string operations return, meaning one can't just write `s[..5]` in an attempt to get the first 5 characters of the string. (Also, there's str::get that returns an Option: https://doc.rust-lang.org/std/primitive.str.html#method.get https://doc.rust-lang.org/std/primitive.str.html#method.get )
- int_19h 8y agoIf you want to just slice on bytes without any String semantics, why not use Vec<u8> then? String implies that it is, well, a string.