pub struct Iter<'a, T: 'a> { /* private fields */ }Expand description
Immutable slice iterator
This struct is created by the iter method on slices.
§Examples
Basic usage:
// First, we need a slice to call the `iter` method on:
let slice = &[1, 2, 3];
// Then we call `iter` on the slice to get the `Iter` iterator,
// and iterate over it:
for element in slice.iter() {
println!("{element}");
}
// This for loop actually already works without calling `iter`:
for element in slice {
println!("{element}");
}Implementations§
Source§impl<'a, T> Iter<'a, T>
impl<'a, T> Iter<'a, T>
1.4.0 · Sourcepub fn as_slice(&self) -> &'a [T]
pub fn as_slice(&self) -> &'a [T]
Views the underlying data as a subslice of the original data.
§Examples
Basic usage:
// First, we need a slice to call the `iter` method on:
let slice = &[1, 2, 3];
// Then we call `iter` on the slice to get the `Iter` iterator:
let mut iter = slice.iter();
// Here `as_slice` still returns the whole slice, so this prints "[1, 2, 3]":
println!("{:?}", iter.as_slice());
// Now, we call the `next` method to remove the first element from the iterator:
iter.next();
// Here the iterator does not contain the first element of the slice any more,
// so `as_slice` only returns the last two elements of the slice,
// and so this prints "[2, 3]":
println!("{:?}", iter.as_slice());
// The underlying slice has not been modified and still contains three elements,
// so this prints "[1, 2, 3]":
println!("{:?}", slice);Trait Implementations§
1.0.0 · Source§impl<T> ExactSizeIterator for Iter<'_, T>
impl<T> ExactSizeIterator for Iter<'_, T>
1.0.0 · Source§impl<'a, T> Iterator for Iter<'a, T>
impl<'a, T> Iterator for Iter<'a, T>
Source§fn next(&mut self) -> Option<&'a T>
fn next(&mut self) -> Option<&'a T>
Advances the iterator and returns the next value. Read more
Source§fn nth(&mut self, n: usize) -> Option<&'a T>
fn nth(&mut self, n: usize) -> Option<&'a T>
Returns the
nth element of the iterator. 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 moreSource§fn fold<B, F>(self, init: B, f: F) -> B
fn fold<B, F>(self, init: B, f: F) -> B
Folds every element into an accumulator by applying an operation,
returning the final result. Read more
Source§fn all<F>(&mut self, f: F) -> bool
fn all<F>(&mut self, f: F) -> bool
Tests if every element of the iterator matches a predicate. Read more
Source§fn any<F>(&mut self, f: F) -> bool
fn any<F>(&mut self, f: F) -> bool
Tests if any element of the iterator matches a predicate. Read more
Source§fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
Searches for an element of an iterator that satisfies a predicate. Read more
Source§fn position<P>(&mut self, predicate: P) -> Option<usize>
fn position<P>(&mut self, predicate: P) -> Option<usize>
Searches for an element in an iterator, returning its index. Read more
1.0.0 · 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
1.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 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
1.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 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(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 more1.0.0 · Source§fn collect<B: FromIterator<Self::Item>>(self) -> Bwhere
Self: Sized,
fn collect<B: FromIterator<Self::Item>>(self) -> Bwhere
Self: Sized,
Transforms an iterator into a collection. Read more
1.27.0 · Source§fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
An iterator method that applies a function as long as it returns
successfully, producing a single, final value. Read more
1.51.0 · Source§fn reduce<F>(self, f: F) -> Option<Self::Item>
fn reduce<F>(self, f: F) -> Option<Self::Item>
Reduces the elements to a single one, by repeatedly applying a reducing
operation. Read more
1.15.0 · Source§fn max_by<F>(self, compare: F) -> Option<Self::Item>
fn max_by<F>(self, compare: F) -> Option<Self::Item>
Returns the element that gives the maximum value with respect to the
specified comparison function. Read more