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 Cairo’s for loop syntax. See also: FromIterator.

Signature

pub trait IntoIterator

Examples

Basic usage:
let mut iter = array![1, 2, 3].into_iter();

assert!(Some(1) == iter.next());
assert!(Some(2) == iter.next());
assert!(Some(3) == iter.next());
assert!(None == iter.next());
Implementing IntoIterator for your type:
// A sample collection, that's just a wrapper over `Array`
#[derive(Drop, Debug)]
struct MyCollection {
    arr: Array
}

// Let's give it some methods so we can create one and add things
// to it.
#[generate_trait]
impl MyCollectionImpl of MyCollectionTrait {
    fn new() -> MyCollection {
        MyCollection {
            arr: ArrayTrait::new()
        }
    }

    fn add(ref self: MyCollection, elem: u32) {
        self.arr.append(elem);
    }
}

// and we'll implement `IntoIterator`
impl MyCollectionIntoIterator of IntoIterator {
    type IntoIter = core::array::ArrayIter;
    fn into_iter(self: MyCollection) -> Self::IntoIter {
        self.arr.into_iter()
    }
}

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

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

// ... and then turn it into an `Iterator`:
let mut n = 0;
for i in c {
    assert!(i == n);
    n += 1;
};

Trait functions

into_iter

Creates an iterator from a value. See the module-level documentation for more.

Signature

fn into_iter(self: T) -> IntoIteratorIntoIter

Examples

let mut iter = array![1, 2, 3].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());

Trait types

IntoIter

The iterator type that will be created.

Signature

type IntoIter;