pub struct CharIndices<'a> { /* private fields */ }Expand description
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§
Source§impl<'a> CharIndices<'a>
impl<'a> CharIndices<'a>
1.4.0 · Sourcepub fn as_str(&self) -> &'a str
pub 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.
1.82.0 · Sourcepub fn offset(&self) -> usize
pub 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§
1.0.0 · Source§impl<'a> Clone for CharIndices<'a>
impl<'a> Clone for CharIndices<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)where
Self:,
fn clone_from(&mut self, source: &Self)where
Self:,
Performs copy-assignment from
source. Read more1.0.0 · Source§impl<'a> Debug for CharIndices<'a>
impl<'a> Debug for CharIndices<'a>
1.0.0 · Source§impl<'a> DoubleEndedIterator for CharIndices<'a>
impl<'a> DoubleEndedIterator for CharIndices<'a>
Source§fn next_back(&mut self) -> Option<(usize, char)>
fn next_back(&mut self) -> Option<(usize, char)>
Removes and returns an element from the end of the iterator. Read more
Source§fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
🔬This is a nightly-only experimental API. (
iter_advance_by #77404)Advances the iterator from the back by
n elements. Read more1.37.0 · Source§fn nth_back(&mut self, n: usize) -> Option<Self::Item>
fn nth_back(&mut self, n: usize) -> Option<Self::Item>
Returns the
nth element from the end of the iterator. Read more1.27.0 · Source§fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
This is the reverse version of
Iterator::try_fold(): it takes
elements starting from the back of the iterator. Read more1.0.0 · Source§impl<'a> Iterator for CharIndices<'a>
impl<'a> Iterator for CharIndices<'a>
Source§fn next(&mut self) -> Option<(usize, char)>
fn next(&mut self) -> Option<(usize, char)>
Advances the iterator and returns the next value. Read more
Source§fn count(self) -> usize
fn count(self) -> usize
Consumes the iterator, counting the number of iterations and returning it. Read more
Source§fn size_hint(&self) -> (usize, Option<usize>)
fn size_hint(&self) -> (usize, Option<usize>)
Returns the bounds on the remaining length of the iterator. Read more
Source§fn last(self) -> Option<(usize, char)>
fn last(self) -> Option<(usize, char)>
Consumes the iterator, returning the last element. Read more
Source§fn next_chunk<const N: usize>(
&mut self,
) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>where
Self: Sized,
fn next_chunk<const N: usize>(
&mut self,
) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>where
Self: Sized,
🔬This is a nightly-only experimental API. (
iter_next_chunk #98326)Advances the iterator and returns an array containing the next
N values. Read moreSource§fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
🔬This is a nightly-only experimental API. (
iter_advance_by #77404)Advances the iterator by
n elements. Read more1.0.0 · Source§fn nth(&mut self, n: usize) -> Option<Self::Item>
fn nth(&mut self, n: usize) -> Option<Self::Item>
Returns the
nth element of the iterator. Read more1.28.0 · Source§fn step_by(self, step: usize) -> StepBy<Self> ⓘwhere
Self: Sized,
fn step_by(self, step: usize) -> StepBy<Self> ⓘwhere
Self: Sized,
Creates an iterator starting at the same point, but stepping by
the given amount at each iteration. Read more
1.0.0 · Source§fn chain<U>(self, other: U) -> Chain<Self, U::IntoIter> ⓘ
fn chain<U>(self, other: U) -> Chain<Self, U::IntoIter> ⓘ
Takes two iterators and creates a new iterator over both in sequence. Read more
1.0.0 · Source§fn zip<U>(self, other: U) -> Zip<Self, U::IntoIter> ⓘwhere
Self: Sized,
U: IntoIterator,
fn zip<U>(self, other: U) -> Zip<Self, U::IntoIter> ⓘwhere
Self: Sized,
U: IntoIterator,
‘Zips up’ two iterators into a single iterator of pairs. Read more
Source§fn intersperse(self, separator: Self::Item) -> Intersperse<Self> ⓘ
fn intersperse(self, separator: Self::Item) -> Intersperse<Self> ⓘ
🔬This is a nightly-only experimental API. (
iter_intersperse #79524)Creates a new iterator which places a copy of
separator between adjacent
items of the original iterator. Read moreSource§fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G> ⓘ
fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G> ⓘ
🔬This is a nightly-only experimental API. (
iter_intersperse #79524)Creates a new iterator which places an item generated by
separator
between adjacent items of the original iterator. Read more1.0.0 · Source§fn map<B, F>(self, f: F) -> Map<Self, F> ⓘ
fn map<B, F>(self, f: F) -> Map<Self, F> ⓘ
Takes a closure and creates an iterator which calls that closure on each
element. Read more
1.0.0 · Source§fn filter<P>(self, predicate: P) -> Filter<Self, P> ⓘ
fn filter<P>(self, predicate: P) -> Filter<Self, P> ⓘ
Creates an iterator which uses a closure to determine if an element
should be yielded. Read more
1.0.0 · Source§fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F> ⓘ
fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F> ⓘ
Creates an iterator that both filters and maps. Read more
1.0.0 · Source§fn enumerate(self) -> Enumerate<Self> ⓘwhere
Self: Sized,
fn enumerate(self) -> Enumerate<Self> ⓘwhere
Self: Sized,
Creates an iterator which gives the current iteration count as well as
the next value. Read more
1.0.0 · Source§fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P> ⓘ
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P> ⓘ
1.0.0 · Source§fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> ⓘ
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> ⓘ
Creates an iterator that yields elements based on a predicate. Read more
1.57.0 · Source§fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P> ⓘ
fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P> ⓘ
Creates an iterator that both yields elements based on a predicate and maps. Read more
1.0.0 · Source§fn skip(self, n: usize) -> Skip<Self> ⓘwhere
Self: Sized,
fn skip(self, n: usize) -> Skip<Self> ⓘwhere
Self: Sized,
Creates an iterator that skips the first
n elements. Read more1.0.0 · Source§fn take(self, n: usize) -> Take<Self> ⓘwhere
Self: Sized,
fn take(self, n: usize) -> Take<Self> ⓘwhere
Self: Sized,
Creates an iterator that yields the first
n elements, or fewer
if the underlying iterator ends sooner. Read more