Skip to content

Commit

Permalink
auto merge of #5818 : Kimundi/rust/iter_to_vec, r=catamorphism
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Apr 10, 2013
2 parents 2c64983 + 24eee52 commit 11f5f73
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,29 @@ pub fn copy_seq<T:Copy,IT:BaseIter<T>,BT:Buildable<T>>(v: &IT) -> BT {
for v.each |x| { push(*x); }
}
}

/**
* Helper function to transform an internal iterator into an owned vector.
*
* # Example:
*
* ~~~
* let v = ~[1, 2, 3];
* let v2 = do iter_to_vec |f| { v.each(|e| f(*e)) };
* if v != v2 { fail!() }
* ~~~
*/
#[inline(always)]
pub fn iter_to_vec<T>(pusher: &fn(it: &fn(T) -> bool)) -> ~[T] {
let mut v = ~[];
let pushf = |e| {v.push(e); true};
pusher(pushf);
v
}

#[test]
fn test_iter_to_vec() {
let v = ~[1, 2, 3];
let v2 = do iter_to_vec |f| { v.each(|e| f(*e)) };
if v != v2 { fail!() }
}

0 comments on commit 11f5f73

Please sign in to comment.