A skeleton code for Iterator in Rust

For record, I wrote down the same code.

Consuming Iterator

pub struct TaskSet;
pub struct TaskSetIterator {
}
impl Iterator for TaskSetIterator {
type Item = Task;
fn next(&mut self) -> Option<Task> {
….
}
}
pub struct Task;
impl IntoIterator for TaskSet {
type Item = Task;
type IntoIter = TaskSetIterator;
fn into_iter(self) -> Self::IntoIter {
TaskSetIterator { … }
}
}

view raw
task.rs
hosted with ❤ by GitHub

Iterator that does not consume items

pub struct TaskSetRefIterator<'a>
{
….
}
impl<'a> Iterator for TaskSetRefIterator<'a> {
type Item = &'a Task;
fn next(&mut self) -> Option<&'a Task> {
None
}
}
impl<'a> IntoIterator for &'a TaskSet {
type Item = &'a Task;
type IntoIter = TaskSetRefIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
TaskSetRefIterator { … }
}
}

view raw
task2.rs
hosted with ❤ by GitHub



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s