pub trait Iterators<'a, T>: IntoIterator<Item = T> {
    type ItemRef: 'a + Deref<Target = T>;
    type ItemMut: 'a + DerefMut<Target = T>;
    type Iter: Iterator<Item = Self::ItemRef>;
    type IterMut: Iterator<Item = Self::ItemMut>;

    fn iter(&'a self) -> Self::Iter;
    fn iter_mut(&'a mut self) -> Self::IterMut;
}
Expand description

Trait that allows to implement all possible iterators over a Collection. All collections must implement all iterators, allowing for default implementations across collections.

There are three different types of iterators:

Safety

All iterators must return the items in the same order as the other iterators. This is required to ensure that the iterators are consistent with each other. This is not checked by the compiler. If this is not upheld, the behavior of the Collection is undefined.

Required Associated Types

The type of reference the immutable iterator (Iter) iterates over the items in the Collection. The reference is only valid for the duration of the iteration.

The type of mutable reference the mutable iterator (IterMut) iterates over the items in the Collection. The reference is only valid for the duration of the iteration.

Which kind of iterator is returned by iter.

Which kind of iterator is returned by iter_mut.

Required Methods

Creates an immutable iterator over the items in the Collection without consuming them.

If you want to be able to modify the collection , you should use the iter_mut method. If you want to consume the collection , you should use the into_iter method that is implemented deriving the IntoIterator trait.

Also, it is recommended to implement the IntoIterator trait but through reference, like this example based on the one at collection :

Examples

Example using the ArrayStack:

use trait_based_collection::{ArrayStack, prelude::*};

let mut collection = ArrayStack::with_capacity(10);
for i in 0..10 {
    collection.add(i);
}
for item in collection.iter() {
    println!("{}", item);
}

Example if the IntoIterator is implemented through reference:

use trait_based_collection::{ArrayStack, prelude::*};

let mut collection = ArrayStack::with_capacity(10);
for i in 0..10 {
    collection.add(i);
}
for item in &collection {
    println!("{}", item);
}

Creates a mutable iterator over the items in the Collection without consuming them.

If you don’t want to be able to modify the collection , you should use the iter method. If you want to consume the collection , you should use the into_iter method that is implemented deriving the IntoIterator trait.

Also, it is recommended to implement the IntoIterator trait but through reference, like this example based on the one at collection :

Examples

Example using the ArrayStack:

use trait_based_collection::{ArrayStack, prelude::*};

let mut collection = ArrayStack::with_capacity(10);
for i in 0..10 {
    collection.add(i);
}
for item in collection.iter_mut() {
    println!("{}", item);
}

Example if the IntoIterator is implemented through reference:

use trait_based_collection::{ArrayStack, prelude::*};

let mut collection = ArrayStack::with_capacity(10);
for i in 0..10 {
    collection.add(i);
}
for item in &collection {
    println!("{}", item);
}

Implementors