Skip to content

Commit 11f5f73

Browse files
committed
auto merge of #5818 : Kimundi/rust/iter_to_vec, r=catamorphism
2 parents 2c64983 + 24eee52 commit 11f5f73

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/libcore/iter.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,29 @@ pub fn copy_seq<T:Copy,IT:BaseIter<T>,BT:Buildable<T>>(v: &IT) -> BT {
344344
for v.each |x| { push(*x); }
345345
}
346346
}
347+
348+
/**
349+
* Helper function to transform an internal iterator into an owned vector.
350+
*
351+
* # Example:
352+
*
353+
* ~~~
354+
* let v = ~[1, 2, 3];
355+
* let v2 = do iter_to_vec |f| { v.each(|e| f(*e)) };
356+
* if v != v2 { fail!() }
357+
* ~~~
358+
*/
359+
#[inline(always)]
360+
pub fn iter_to_vec<T>(pusher: &fn(it: &fn(T) -> bool)) -> ~[T] {
361+
let mut v = ~[];
362+
let pushf = |e| {v.push(e); true};
363+
pusher(pushf);
364+
v
365+
}
366+
367+
#[test]
368+
fn test_iter_to_vec() {
369+
let v = ~[1, 2, 3];
370+
let v2 = do iter_to_vec |f| { v.each(|e| f(*e)) };
371+
if v != v2 { fail!() }
372+
}

0 commit comments

Comments
 (0)