Skip to content

Commit 883d583

Browse files
committed
iterator: reuse iter::to_vec, and use &mut self
1 parent ea8a55b commit 883d583

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

src/libcore/iterator.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub trait IteratorUtil<A> {
4747
fn advance(&mut self, f: &fn(A) -> bool);
4848
#[cfg(not(stage0))]
4949
fn advance(&mut self, f: &fn(A) -> bool) -> bool;
50-
fn to_vec(self) -> ~[A];
50+
fn to_vec(&mut self) -> ~[A];
5151
fn nth(&mut self, n: uint) -> Option<A>;
5252
fn last(&mut self) -> Option<A>;
5353
fn fold<B>(&mut self, start: B, f: &fn(B, A) -> B) -> B;
@@ -147,11 +147,8 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
147147
}
148148

149149
#[inline(always)]
150-
fn to_vec(self) -> ~[A] {
151-
let mut v = ~[];
152-
let mut it = self;
153-
for it.advance() |x| { v.push(x); }
154-
return v;
150+
fn to_vec(&mut self) -> ~[A] {
151+
iter::to_vec::<A>(|f| self.advance(f))
155152
}
156153

157154
/// Return the `n`th item yielded by an iterator.
@@ -563,7 +560,7 @@ mod tests {
563560

564561
#[test]
565562
fn test_filter_map() {
566-
let it = Counter::new(0u, 1u).take(10)
563+
let mut it = Counter::new(0u, 1u).take(10)
567564
.filter_map(|x: uint| if x.is_even() { Some(x*x) } else { None });
568565
assert_eq!(it.to_vec(), ~[0*0, 2*2, 4*4, 6*6, 8*8]);
569566
}

0 commit comments

Comments
 (0)