Struct CharIndices

struct CharIndices<'a> { ... }

An iterator over the chars of a string slice, and their positions.

This struct is created by the char_indices method on str. See its documentation for more.

Implementations

impl<'a> CharIndices<'a>

fn as_str(self: &Self) -> &'a str

Views the underlying data as a subslice of the original data.

This has the same lifetime as the original slice, and so the iterator can continue to be used while this exists.

fn offset(self: &Self) -> usize

Returns the byte position of the next character, or the length of the underlying string if there are no more characters.

This means that, when the iterator has not been fully consumed, the returned value will match the index that will be returned by the next call to next().

Examples

let mut chars = "a楽".char_indices();

// `next()` has not been called yet, so `offset()` returns the byte
// index of the first character of the string, which is always 0.
assert_eq!(chars.offset(), 0);
// As expected, the first call to `next()` also returns 0 as index.
assert_eq!(chars.next(), Some((0, 'a')));

// `next()` has been called once, so `offset()` returns the byte index
// of the second character ...
assert_eq!(chars.offset(), 1);
// ... which matches the index returned by the next call to `next()`.
assert_eq!(chars.next(), Some((1, '')));

// Once the iterator has been consumed, `offset()` returns the length
// in bytes of the string.
assert_eq!(chars.offset(), 4);
assert_eq!(chars.next(), None);

impl FusedIterator for CharIndices<'_>

impl<'a> Clone for CharIndices<'a>

fn clone(self: &Self) -> CharIndices<'a>

impl<'a> Debug for CharIndices<'a>

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl<'a> DoubleEndedIterator for CharIndices<'a>

fn next_back(self: &mut Self) -> Option<(usize, char)>

impl<'a> Freeze for CharIndices<'a>

impl<'a> Iterator for CharIndices<'a>

fn next(self: &mut Self) -> Option<(usize, char)>
fn count(self: Self) -> usize
fn size_hint(self: &Self) -> (usize, Option<usize>)
fn last(self: Self) -> Option<(usize, char)>

impl<'a> RefUnwindSafe for CharIndices<'a>

impl<'a> Send for CharIndices<'a>

impl<'a> Sync for CharIndices<'a>

impl<'a> Unpin for CharIndices<'a>

impl<'a> UnsafeUnpin for CharIndices<'a>

impl<'a> UnwindSafe for CharIndices<'a>

impl<I> IntoIterator for CharIndices<'a>

fn into_iter(self: Self) -> I

impl<T> Any for CharIndices<'a>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for CharIndices<'a>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for CharIndices<'a>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for CharIndices<'a>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for CharIndices<'a>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for CharIndices<'a>

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for CharIndices<'a>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for CharIndices<'a>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>