Yes it is… And it’s not for the fact that one has to “wrap” his mind to use iterator operations like let mut inices = string.char_indices(); let start_byte = indices.nth(start).unwrap(); let stop_byte = indices.nth(stop - start).unwrap(), it’s for the fact that the previous snippet – as explained in my original post – is incorrect, and the “correct” solution is almost a page long (and I’m sure I got some bugs in there…)
I promise this is the last time I make an attempt in “defending” “theoretically incorrect” API’s, which when used wrong could lead to obnoxious bugs or even worse to crashes, but let’s leave code-point boundaries for a minute and think about the following: I bet there are countless bugs caused by division by zero, however this didn’t prompt any programming language (or standard library) from “forbidding” either the number zero or the division operation. Instead the developers have to check and sanitize their inputs, just like a developer that slices a string on a character basis should have in mind.
To put it from another perspective: from what I understand the main issue with code-point slicing is that there are “characters” that are made of multiple code-points, and slicing a string in the middle of a character like that would lead to bugs.
Then if this is the case then I bet such situations are easy to check and guard against, by changing the signature of a “would be” slicing method to str::get_char_boundary(Range) -> Result<str, CharBoundaryError>, where CharBoundaryError would be an enum that has arms like OutOfRange, CharacterIsMultiCodePoint, etc.
In this way we get a “correct” and useful API at the same time.