Trait trait_based_collection::collection::Iterators
source · [−]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:
iter
returns an immutable iterator overItemRef
in theCollection
without consuming them.iter_mut
returns a mutable iterator over theItemMut
in theCollection
without consuming them.into_iter
returns an iterator over the items in theCollection
and consumes the collection .
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
sourcetype ItemRef: 'a + Deref<Target = T>
type ItemRef: 'a + Deref<Target = T>
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.
sourcetype ItemMut: 'a + DerefMut<Target = T>
type ItemMut: 'a + DerefMut<Target = T>
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.
Required Methods
sourcefn iter(&'a self) -> Self::Iter
fn iter(&'a self) -> Self::Iter
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);
}
sourcefn iter_mut(&'a mut self) -> Self::IterMut
fn iter_mut(&'a mut self) -> Self::IterMut
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);
}