Struct IntoIter

struct IntoIter<T, N: usize> { ... }

A by-value [array] iterator.

Implementations

impl<T, N: usize> IntoIter<T, N>

fn new(array: [T; N]) -> Self

Creates a new iterator over the given array.

unsafe const fn new_unchecked(buffer: [MaybeUninit<T>; N], initialized: Range<usize>) -> Self

Creates an iterator over the elements in a partially-initialized buffer.

If you have a fully-initialized array, then use IntoIterator. But this is useful for returning partial results from unsafe code.

Safety

  • The buffer[initialized] elements must all be initialized.
  • The range must be canonical, with initialized.start <= initialized.end.
  • The range must be in-bounds for the buffer, with initialized.end <= N. (Like how indexing [0][100..100] fails despite the range being empty.)

It's sound to have more elements initialized than mentioned, though that will most likely result in them being leaked.

Examples

#![feature(array_into_iter_constructors)]
#![feature(maybe_uninit_uninit_array_transpose)]
use std::array::IntoIter;
use std::mem::MaybeUninit;

# // Hi!  Thanks for reading the code. This is restricted to `Copy` because
# // otherwise it could leak. A fully-general version this would need a drop
# // guard to handle panics from the iterator, but this works for an example.
fn next_chunk<T: Copy, const N: usize>(
    it: &mut impl Iterator<Item = T>,
) -> Result<[T; N], IntoIter<T, N>> {
    let mut buffer = [const { MaybeUninit::uninit() }; N];
    let mut i = 0;
    while i < N {
        match it.next() {
            Some(x) => {
                buffer[i].write(x);
                i += 1;
            }
            None => {
                // SAFETY: We've initialized the first `i` items
                unsafe {
                    return Err(IntoIter::new_unchecked(buffer, 0..i));
                }
            }
        }
    }

    // SAFETY: We've initialized all N items
    unsafe { Ok(buffer.transpose().assume_init()) }
}

let r: [_; 4] = next_chunk(&mut (10..16)).unwrap();
assert_eq!(r, [10, 11, 12, 13]);
let r: IntoIter<_, 40> = next_chunk(&mut (10..16)).unwrap_err();
assert_eq!(r.collect::<Vec<_>>(), vec![10, 11, 12, 13, 14, 15]);
const fn empty() -> Self

Creates an iterator over T which returns no elements.

If you just need an empty iterator, then use iter::empty() instead. And if you need an empty array, use [].

But this is useful when you need an array::IntoIter<T, N> specifically.

Examples

#![feature(array_into_iter_constructors)]
use std::array::IntoIter;

let empty = IntoIter::<i32, 3>::empty();
assert_eq!(empty.len(), 0);
assert_eq!(empty.as_slice(), &[]);

let empty = IntoIter::<std::convert::Infallible, 200>::empty();
assert_eq!(empty.len(), 0);

[1, 2].into_iter() and [].into_iter() have different types

#![feature(array_into_iter_constructors)]
use std::array::IntoIter;

pub fn get_bytes(b: bool) -> IntoIter<i8, 4> {
    if b {
        [1, 2, 3, 4].into_iter()
    } else {
        [].into_iter() // error[E0308]: mismatched types
    }
}

But using this method you can get an empty iterator of appropriate size:

#![feature(array_into_iter_constructors)]
use std::array::IntoIter;

pub fn get_bytes(b: bool) -> IntoIter<i8, 4> {
    if b {
        [1, 2, 3, 4].into_iter()
    } else {
        IntoIter::empty()
    }
}

assert_eq!(get_bytes(true).collect::<Vec<_>>(), vec![1, 2, 3, 4]);
assert_eq!(get_bytes(false).collect::<Vec<_>>(), vec![]);
fn as_slice(self: &Self) -> &[T]

Returns an immutable slice of all elements that have not been yielded yet.

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

Returns a mutable slice of all elements that have not been yielded yet.

impl<I> IntoIterator for IntoIter<T, N>

fn into_iter(self: Self) -> I

impl<T> Any for IntoIter<T, N>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for IntoIter<T, N>

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

impl<T> BorrowMut for IntoIter<T, N>

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

impl<T> CloneToUninit for IntoIter<T, N>

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

impl<T> From for IntoIter<T, N>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, N: usize> Default for IntoIter<T, N>

fn default() -> Self

impl<T, N: usize> DoubleEndedIterator for IntoIter<T, N>

fn next_back(self: &mut Self) -> Option<<Self as >::Item>
fn rfold<Acc, Fold>(self: Self, init: Acc, rfold: Fold) -> Acc
where
    Fold: FnMut(Acc, <Self as >::Item) -> Acc
fn try_rfold<B, F, R>(self: &mut Self, init: B, f: F) -> R
where
    Self: Sized,
    F: FnMut(B, <Self as >::Item) -> R,
    R: Try<Output = B>
fn advance_back_by(self: &mut Self, n: usize) -> Result<(), NonZero<usize>>

impl<T, N: usize> Drop for IntoIter<T, N>

fn drop(self: &mut Self)

impl<T, N: usize> ExactSizeIterator for IntoIter<T, N>

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

impl<T, N: usize> Freeze for IntoIter<T, N>

impl<T, N: usize> FusedIterator for IntoIter<T, N>

impl<T, N: usize> Iterator for IntoIter<T, N>

fn next(self: &mut Self) -> Option<<Self as >::Item>
fn size_hint(self: &Self) -> (usize, Option<usize>)
fn fold<Acc, Fold>(self: Self, init: Acc, fold: Fold) -> Acc
where
    Fold: FnMut(Acc, <Self as >::Item) -> Acc
fn try_fold<B, F, R>(self: &mut Self, init: B, f: F) -> R
where
    Self: Sized,
    F: FnMut(B, <Self as >::Item) -> R,
    R: Try<Output = B>
fn count(self: Self) -> usize
fn last(self: Self) -> Option<<Self as >::Item>
fn advance_by(self: &mut Self, n: usize) -> Result<(), NonZero<usize>>

impl<T, N: usize> RefUnwindSafe for IntoIter<T, N>

impl<T, N: usize> Send for IntoIter<T, N>

impl<T, N: usize> Sync for IntoIter<T, N>

impl<T, N: usize> TrustedLen for IntoIter<T, N>

impl<T, N: usize> Unpin for IntoIter<T, N>

impl<T, N: usize> UnsafeUnpin for IntoIter<T, N>

impl<T, N: usize> UnwindSafe for IntoIter<T, N>

impl<T, U> Into for IntoIter<T, N>

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 IntoIter<T, N>

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

impl<T, U> TryInto for IntoIter<T, N>

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

impl<T: $crate::clone::Clone, N: usize> Clone for IntoIter<T, N>

fn clone(self: &Self) -> IntoIter<T, N>

impl<T: fmt::Debug, N: usize> Debug for IntoIter<T, N>

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