I am aware of https://doc.rust-lang.org/std/string/struct.String.html
I can not find a function ‘substring’ where it takes two args, start (inclusive) nd end (exclusive), and returns a &str pointing to the cahrs in between.
Does this string not exist due to the “char can be 1, 2, or 4 bytes” ?
EDIT: indices are specified in chars, not utf8 bytes
String b = "abc".to_string(); let substring = &b[start..end];
LOL, all this time I viewed &str as a special construct rather than a slice of chars. Thanks!
all this time I viewed &str as a special construct rather than a slice of chars.
Close, but you have to remember that (a) it is a primitive, and not a slice, although it can be casted to a slice for free, and (b) it’s a slice of bytes, not a slice of chars.
It’s not a slice of
chars. It’s a slice of
u8s which must be valid UTF-8.
If you’re looking for
char-based substring, that doesn’t exist. You’d need to turn the
char indices into byte indices, then slice on that.
- If you want to get the “first n characters” or something then your best bet is to use
- Strings are guaranteed to be UTF-8 encoded.
- String slicing is equivalent to slicing the underlying array (i.e. the indices are for bytes not for chars).
- Rust will panic if you try to slice the string such that you end up with invalid UTF-8.
chars don’t necessarily correspond to a user’s idea of what a single “character” is, either.