Implemented Items<'a, T> for List<T>
This commit is contained in:
parent
2b362768ff
commit
52524bfe88
1 changed files with 46 additions and 0 deletions
|
@ -17,6 +17,42 @@ pub enum List<T> {
|
|||
Nil,
|
||||
}
|
||||
|
||||
pub struct Items<'a, T> {
|
||||
priv head: &'a List<T>,
|
||||
priv next: Option<&'a @List<T>>
|
||||
}
|
||||
|
||||
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<T> List<T> {
|
||||
/// 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<int> = @List::from_vec([]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue