core::iter

Trait IntoIterator

1.6.0 · Source
pub trait IntoIterator {
    type Item;
    type IntoIter: Iterator<Item = Self::Item>;

    // Required method
    fn into_iter(self) -> Self::IntoIter;
}
Expand description

Conversion into an Iterator.

By implementing IntoIterator for a type, you define how it will be converted to an iterator. This is common for types which describe a collection of some kind.

One benefit of implementing IntoIterator is that your type will work with Rust’s for loop syntax.

See also: FromIterator.

§Examples

Basic usage:

let v = [1, 2, 3];
let mut iter = v.into_iter();

assert_eq!(Some(1), iter.next());
assert_eq!(Some(2), iter.next());
assert_eq!(Some(3), iter.next());
assert_eq!(None, iter.next());

Implementing IntoIterator for your type:

// A sample collection, that's just a wrapper over Vec<T>
#[derive(Debug)]
struct MyCollection(Vec<i32>);

// Let's give it some methods so we can create one and add things
// to it.
impl MyCollection {
    fn new() -> MyCollection {
        MyCollection(Vec::new())
    }

    fn add(&mut self, elem: i32) {
        self.0.push(elem);
    }
}

// and we'll implement IntoIterator
impl IntoIterator for MyCollection {
    type Item = i32;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

// Now we can make a new collection...
let mut c = MyCollection::new();

// ... add some stuff to it ...
c.add(0);
c.add(1);
c.add(2);

// ... and then turn it into an Iterator:
for (i, n) in c.into_iter().enumerate() {
    assert_eq!(i as i32, n);
}

It is common to use IntoIterator as a trait bound. This allows the input collection type to change, so long as it is still an iterator. Additional bounds can be specified by restricting on Item:

fn collect_as_strings<T>(collection: T) -> Vec<String>
where
    T: IntoIterator,
    T::Item: std::fmt::Debug,
{
    collection
        .into_iter()
        .map(|item| format!("{item:?}"))
        .collect()
}

Required Associated Types§

1.6.0 · Source

type Item

The type of the elements being iterated over.

1.6.0 · Source

type IntoIter: Iterator<Item = Self::Item>

Which kind of iterator are we turning this into?

Required Methods§

1.6.0 · Source

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value.

See the module-level documentation for more.

§Examples
let v = [1, 2, 3];
let mut iter = v.into_iter();

assert_eq!(Some(1), iter.next());
assert_eq!(Some(2), iter.next());
assert_eq!(Some(3), iter.next());
assert_eq!(None, iter.next());

Implementors§

1.6.0 · Source§

impl<'a, T> IntoIterator for &'a Option<T>

1.6.0 · Source§

impl<'a, T> IntoIterator for &'a [T]

1.6.0 · Source§

impl<'a, T> IntoIterator for &'a mut Option<T>

1.6.0 · Source§

impl<'a, T> IntoIterator for &'a mut [T]

1.6.0 · Source§

impl<'a, T, E> IntoIterator for &'a Result<T, E>

1.6.0 · Source§

impl<'a, T, E> IntoIterator for &'a mut Result<T, E>

1.6.0 · Source§

impl<'a, T, const N: usize> IntoIterator for &'a [T; N]

1.6.0 · Source§

impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]

Source§

impl<A: Step> IntoIterator for Range<A>

Source§

impl<A: Step> IntoIterator for RangeFrom<A>

Source§

impl<A: Step> IntoIterator for RangeInclusive<A>

1.6.0 · Source§

impl<I: Iterator> IntoIterator for I

1.6.0 · Source§

impl<T> IntoIterator for Option<T>

1.6.0 · Source§

impl<T, E> IntoIterator for Result<T, E>

1.53.0 · Source§

impl<T, const N: usize> IntoIterator for [T; N]