4 ms·
> because of the way UTF-8 is encoded. Yes, I know, I mention this in my blog post :) Rust's indexing methods on strings actually do operate on byte indices.
by Manishearth 10y ago
> because of the way UTF-8 is encoded.
Yes, I know, I mention this in my blog post :)
Rust's indexing methods on strings actually do operate on byte indices. You can't do `"foo"[2]`, but you can do `"foo[2..3]"`, and it will panic if you are not on a boundary. Nobody uses this (like you say, it's troublesome) :P I've seen folks just break out the byte/char iterator whenever they need something more complicated, because usually they do need to iterate anyway.
Rust internally stores slices as byte indices (well, pointer + byte length), but the compiler enforces the validity of that.