Iter

Struct Iter 

1.6.0 · Source
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>

1.4.0 · Source

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>

Source§

fn len(&self) -> usize

Ferrocene addition: Gate default implementation
Source§

fn is_empty(&self) -> bool

🔬This is a nightly-only experimental API. (exact_size_is_empty #35428)
Returns true if the iterator is empty. Read more
1.0.0 · Source§

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

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

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>

Returns the nth element of the iterator. Read more
Source§

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 more
Source§

fn fold<B, F>(self, init: B, f: F) -> B
where F: FnMut(B, Self::Item) -> B,

Folds every element into an accumulator by applying an operation, returning the final result. Read more
Source§

fn for_each<F>(self, f: F)
where Self: Sized, F: FnMut(Self::Item),

Calls a closure on each element of an iterator. Read more
Source§

fn all<F>(&mut self, f: F) -> bool
where Self: Sized, F: FnMut(Self::Item) -> bool,

Tests if every element of the iterator matches a predicate. Read more
Source§

fn any<F>(&mut self, f: F) -> bool
where Self: Sized, F: FnMut(Self::Item) -> bool,

Tests if any element of the iterator matches a predicate. Read more
Source§

fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Searches for an element of an iterator that satisfies a predicate. Read more
Source§

fn position<P>(&mut self, predicate: P) -> Option<usize>
where Self: Sized, P: FnMut(Self::Item) -> bool,

Searches for an element in an iterator, returning its index. Read more
1.0.0 · Source§

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,

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,

‘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>
where Self: Sized, F: FnMut(Self::Item) -> B,

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>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

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,

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,

Creates an iterator that skips the first n elements. Read more
1.0.0 · Source§

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
1.0.0 · Source§

fn collect<B: FromIterator<Self::Item>>(self) -> B
where 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
where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Output = B>,

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>
where Self: Sized, F: FnMut(Self::Item, Self::Item) -> 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>
where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,

Returns the element that gives the maximum value with respect to the specified comparison function. Read more
1.0.0 · Source§

fn cloned<'a, T>(self) -> Cloned<Self>
where T: Clone + 'a, Self: Sized + Iterator<Item = &'a T>,

Creates an iterator which clones all of its elements. Read more
1.11.0 · Source§

fn sum<S>(self) -> S
where Self: Sized, S: Sum<Self::Item>,

Sums the elements of an iterator. Read more
1.0.0 · Source§

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

1.0.0 · Source§

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

Source§

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

Auto Trait Implementations§

§

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

§

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

Blanket Implementations§

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<I> IntoIterator for I
where I: Iterator,

Source§

type Item = <I as Iterator>::Item

The type of the elements being iterated over.
Source§

type IntoIter = I

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> I

Creates an iterator from a value. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.