Struct Iter

struct Iter<'a, T: 'a> { ... }

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

impl<'a, T> Iter<'a, T>

fn as_slice(self: &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);

impl<'a, T> DoubleEndedIterator for Iter<'a, T>

fn next_back(self: &mut Self) -> Option<&'a T>
fn nth_back(self: &mut Self, n: usize) -> Option<&'a T>
fn advance_back_by(self: &mut Self, n: usize) -> Result<(), NonZero<usize>>

impl<'a, T> Freeze for Iter<'a, T>

impl<'a, T> Iterator for Iter<'a, T>

fn next(self: &mut Self) -> Option<&'a T>
fn next_chunk<N: usize>(self: &mut Self) -> Result<[&'a T; N], IntoIter<&'a T, N>>
fn size_hint(self: &Self) -> (usize, Option<usize>)
fn count(self: Self) -> usize
fn nth(self: &mut Self, n: usize) -> Option<&'a T>
fn advance_by(self: &mut Self, n: usize) -> Result<(), NonZero<usize>>
fn last(self: Self) -> Option<&'a T>
fn fold<B, F>(self: Self, init: B, f: F) -> B
where
    F: FnMut(B, <Self as >::Item) -> B
fn for_each<F>(self: Self, f: F)
where
    Self: Sized,
    F: FnMut(<Self as >::Item)
fn all<F>(self: &mut Self, f: F) -> bool
where
    Self: Sized,
    F: FnMut(<Self as >::Item) -> bool
fn any<F>(self: &mut Self, f: F) -> bool
where
    Self: Sized,
    F: FnMut(<Self as >::Item) -> bool
fn find<P>(self: &mut Self, predicate: P) -> Option<<Self as >::Item>
where
    Self: Sized,
    P: FnMut(&<Self as >::Item) -> bool
fn find_map<B, F>(self: &mut Self, f: F) -> Option<B>
where
    Self: Sized,
    F: FnMut(<Self as >::Item) -> Option<B>
fn position<P>(self: &mut Self, predicate: P) -> Option<usize>
where
    Self: Sized,
    P: FnMut(<Self as >::Item) -> bool
fn rposition<P>(self: &mut Self, predicate: P) -> Option<usize>
where
    P: FnMut(<Self as >::Item) -> bool,
    Self: Sized + ExactSizeIterator + DoubleEndedIterator
fn is_sorted_by<F>(self: Self, compare: F) -> bool
where
    Self: Sized,
    F: FnMut(&<Self as >::Item, &<Self as >::Item) -> bool

impl<'a, T> RefUnwindSafe for Iter<'a, T>

impl<'a, T> Unpin for Iter<'a, T>

impl<'a, T> UnsafeUnpin for Iter<'a, T>

impl<'a, T> UnwindSafe for Iter<'a, T>

impl<I> IntoIterator for Iter<'a, T>

fn into_iter(self: Self) -> I

impl<T> Any for Iter<'a, T>

fn type_id(self: &Self) -> TypeId

impl<T> AsRef for Iter<'_, T>

fn as_ref(self: &Self) -> &[T]

impl<T> Borrow for Iter<'a, T>

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

impl<T> BorrowMut for Iter<'a, T>

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

impl<T> Clone for Iter<'_, T>

fn clone(self: &Self) -> Self

impl<T> CloneToUninit for Iter<'a, T>

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

impl<T> Default for Iter<'_, T>

fn default() -> Self

Creates an empty slice iterator.

# use core::slice::Iter;
let iter: Iter < '_, u8 > = Default::default();
assert_eq!(iter.len(), 0);

impl<T> ExactSizeIterator for Iter<'_, T>

fn len(self: &Self) -> usize
fn is_empty(self: &Self) -> bool

impl<T> From for Iter<'a, T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> FusedIterator for Iter<'_, T>

impl<T> TrustedLen for Iter<'_, T>

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

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 Iter<'a, T>

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

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

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

impl<T: Sync> Send for Iter<'_, T>

impl<T: Sync> Sync for Iter<'_, T>

impl<T: fmt::Debug> Debug for Iter<'_, T>

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