Skip to content

Commit 2f1a13c

Browse files
committed
---
yaml --- r: 106473 b: refs/heads/try c: 52524bf h: refs/heads/master i: 106471: 875cab5 v: v3
1 parent a46335b commit 2f1a13c

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: b8ef9fd9c9f642ce7b8aed82782a1ed745d08d64
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: b8601a3d8b91ad3b653d143307611f2f5c75617e
5-
refs/heads/try: 2b362768ff3cab2c966f1f18cf119b21fc96ea30
5+
refs/heads/try: 52524bfe880f3722d8d70e4433429c1b4a3f31d3
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libcollections/list.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,42 @@ pub enum List<T> {
1717
Nil,
1818
}
1919

20+
pub struct Items<'a, T> {
21+
priv head: &'a List<T>,
22+
priv next: Option<&'a @List<T>>
23+
}
24+
25+
impl<'a, T> Iterator<&'a T> for Items<'a, T> {
26+
fn next(&mut self) -> Option<&'a T> {
27+
match self.next {
28+
None => match *self.head {
29+
Nil => None,
30+
Cons(ref value, ref tail) => {
31+
self.next = Some(tail);
32+
Some(value)
33+
}
34+
},
35+
Some(next) => match **next {
36+
Nil => None,
37+
Cons(ref value, ref tail) => {
38+
self.next = Some(tail);
39+
Some(value)
40+
}
41+
}
42+
}
43+
}
44+
}
45+
46+
impl<T> List<T> {
47+
/// Returns a forward iterator
48+
pub fn iter<'a>(&'a self) -> Items<'a, T> {
49+
Items {
50+
head: self,
51+
next: None
52+
}
53+
}
54+
}
55+
2056
/**
2157
* Left fold
2258
*
@@ -181,6 +217,16 @@ mod tests {
181217

182218
use std::option;
183219

220+
#[test]
221+
fn test_iter() {
222+
let list = List::from_vec([0, 1, 2]);
223+
let mut iter = list.iter();
224+
assert_eq!(&0, iter.next().unwrap());
225+
assert_eq!(&1, iter.next().unwrap());
226+
assert_eq!(&2, iter.next().unwrap());
227+
assert_eq!(None, iter.next());
228+
}
229+
184230
#[test]
185231
fn test_is_empty() {
186232
let empty : @list::List<int> = @List::from_vec([]);

0 commit comments

Comments
 (0)