A skeleton code for Iterator in Rust
Posted: October 13, 2015 Filed under: Code | Tags: iterator, rust Leave a commentFor record, I wrote down the same code.
Consuming Iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { ... } | |
} | |
} |
Iterator that does not consume items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { ... } | |
} | |
} |