diff options
| author | Marvin Löbel <loebel.marvin@gmail.com> | 2013-04-10 16:57:52 +0200 |
|---|---|---|
| committer | Marvin Löbel <loebel.marvin@gmail.com> | 2013-04-10 16:57:52 +0200 |
| commit | 24eee5296bd8ec29285f65ef180d9cc9a9a391bf (patch) | |
| tree | 0626899fac7fade3b03b1098cffddacb3246bdcb | |
| parent | 72228012343db793ff453b7cf038d973ccac61a7 (diff) | |
| download | rust-24eee5296bd8ec29285f65ef180d9cc9a9a391bf.tar.gz rust-24eee5296bd8ec29285f65ef180d9cc9a9a391bf.zip | |
Added iter_to_vec conversion fn
| -rw-r--r-- | src/libcore/iter.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 7ed4c3c36ea..9c704fbd699 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -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!() } +} |
