Struct CharIndices

#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct CharIndices<'a> { pub(in ::str) front_offset: usize, pub(in ::str) iter: Chars<'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.

Fields

front_offset: usize
iter: Chars<'a>

Implementations

impl<'a> CharIndices<'a>

fn as_str(&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) -> 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);

Trait Implementations

impl FusedIterator for CharIndices<'_>

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

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

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

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

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

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

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

type Item = (usize, char);
fn next(&mut self) -> Option<(usize, char)>
fn count(self) -> usize
fn size_hint(&self) -> (usize, Option<usize>)
fn last(self) -> Option<(usize, char)>

Auto Trait Implementations

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

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>

Blanket Implementations

impl<I> IntoIterator for CharIndices<'a> where I: Iterator,

type Item = <I as Iterator>::Item;
type IntoIter = I;
fn into_iter(self) -> I

impl<T> Any for CharIndices<'a> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for CharIndices<'a> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for CharIndices<'a> where T: ?Sized,

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

impl<T> CloneToUninit for CharIndices<'a> where T: Clone,

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

impl<T> From<T> for CharIndices<'a>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for CharIndices<'a> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for CharIndices<'a>

impl<T, U> Into<U> for CharIndices<'a> where U: From<T>,

fn into(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<U> for CharIndices<'a> where U: Into<T>,

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

impl<T, U> TryInto<U> for CharIndices<'a> where U: TryFrom<T>,

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