CharIndices should have a way to access the current offset from the beginning

The CharIndices iterator has a field internally called front_offset, that I think would be very useful to have access to.

You can already do something like 'char_indices.next().map(|(offset, _)| offset)', but that is wordy, in addition to not handling the case where the iterator has ended, where you'd want the offset to be equal to the length.

I think it would be an easy and quite handy addition to add a method to CharIndices, maybe called 'offset' or something, that just gives back the starting offset of the next character.

1 Like

You can also add c.len_utf8() to the previously returned index. You don't need to call next.

This seems like a reasonable request. You could submit a pull request to add this as an unstable method to the standard library.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.