diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 8be06bc9afea0..d4685f37a39d4 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -17,6 +17,42 @@ pub enum List { Nil, } +pub struct Items<'a, T> { + priv head: &'a List, + priv next: Option<&'a @List> +} + +impl<'a, T> Iterator<&'a T> for Items<'a, T> { + fn next(&mut self) -> Option<&'a T> { + match self.next { + None => match *self.head { + Nil => None, + Cons(ref value, ref tail) => { + self.next = Some(tail); + Some(value) + } + }, + Some(next) => match **next { + Nil => None, + Cons(ref value, ref tail) => { + self.next = Some(tail); + Some(value) + } + } + } + } +} + +impl List { + /// Returns a forward iterator + pub fn iter<'a>(&'a self) -> Items<'a, T> { + Items { + head: self, + next: None + } + } +} + /** * Left fold * @@ -181,6 +217,16 @@ mod tests { use std::option; + #[test] + fn test_iter() { + let list = List::from_vec([0, 1, 2]); + let mut iter = list.iter(); + assert_eq!(&0, iter.next().unwrap()); + assert_eq!(&1, iter.next().unwrap()); + assert_eq!(&2, iter.next().unwrap()); + assert_eq!(None, iter.next()); + } + #[test] fn test_is_empty() { let empty : @list::List = @List::from_vec([]);