Struct Iter

#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Iter<'a, T: 'a> { pub(in ::slice::iter) ptr: NonNull<T>, pub(in ::slice::iter) end_or_len: *const T, pub(in ::slice::iter) _marker: PhantomData<&'a T> }

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}");
}

Fields

ptr: NonNull<T>

The pointer to the next element to return, or the past-the-end location if the iterator is empty.

This address will be used for all ZST elements, never changed.

end_or_len: *const T

For non-ZSTs, the non-null pointer to the past-the-end element.

For ZSTs, this is ptr::without_provenance_mut(len).

_marker: PhantomData<&'a T>

Implementations

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

unsafe fn next_back_unchecked(&mut self) -> &'a T

Returns the last element and moves the end of the iterator backwards by 1.

Safety

The iterator must not be empty

fn make_slice(&self) -> &'a [T]
unsafe fn post_inc_start(&mut self, offset: usize) -> NonNull<T>
unsafe fn pre_dec_end(&mut self, offset: usize) -> NonNull<T>

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

const fn new(slice: &'a [T]) -> Self
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

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

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

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

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

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

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

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl<'a, const N: usize, T> SpecNextChunk<'a, N, T> for Iter<'a, T> where T: Copy + 'a,

fn spec_next_chunk(&mut self) -> Result<[T; N], IntoIter<T, N>>

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

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

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

fn clone(&self) -> Self

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) -> usize
fn is_empty(&self) -> bool

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

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

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

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

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

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

Auto Trait Implementations

impl<'a, T> Freeze for Iter<'a, T> where NonNull<T>: Freeze, *const T: Freeze, PhantomData<&'a T>: Freeze,

impl<'a, T> RefUnwindSafe for Iter<'a, T> where NonNull<T>: RefUnwindSafe, *const T: RefUnwindSafe, PhantomData<&'a T>: RefUnwindSafe,

impl<'a, T> Unpin for Iter<'a, T> where NonNull<T>: Unpin, *const T: Unpin, PhantomData<&'a T>: Unpin,

impl<'a, T> UnsafeUnpin for Iter<'a, T> where NonNull<T>: UnsafeUnpin, *const T: UnsafeUnpin, PhantomData<&'a T>: UnsafeUnpin,

impl<'a, T> UnwindSafe for Iter<'a, T> where NonNull<T>: UnwindSafe, *const T: UnwindSafe, PhantomData<&'a T>: UnwindSafe,

Blanket Implementations

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

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

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

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

fn from(t: T) -> T

Returns the argument unchanged.

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

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

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

impl<T, U> Into<U> for Iter<'a, T> 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 Iter<'a, T> where U: Into<T>,

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

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

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