From 2e0593d9999a50c74ea2962e53b8f5686037fd36 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sat, 29 Oct 2011 17:23:58 -0700 Subject: [PATCH] stdlib: Add vec::concat to concatenate a vector of vectors Compare to str::concat --- src/lib/vec.rs | 13 +++++++++++++ src/test/stdtest/vec.rs | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/lib/vec.rs b/src/lib/vec.rs index 548afaa73bc9..f459a75270a4 100644 --- a/src/lib/vec.rs +++ b/src/lib/vec.rs @@ -446,6 +446,19 @@ fn filter(f: block(T) -> bool, v: [mutable? T]) -> [T] { ret result; } +/* +Function: concat + +Concatenate a vector of vectors. Flattens a vector of vectors of T into +a single vector of T. +*/ +fn concat(v: [mutable? [mutable? T]]) -> [T] { + // FIXME: So much copying + let new: [T] = []; + for inner: [T] in v { new += inner; } + ret new; +} + /* Function: foldl diff --git a/src/test/stdtest/vec.rs b/src/test/stdtest/vec.rs index 5f644c450b6d..1bbb003e1c4e 100644 --- a/src/test/stdtest/vec.rs +++ b/src/test/stdtest/vec.rs @@ -469,6 +469,11 @@ fn init_empty() { #[ignore] fn init_empty() { } +#[test] +fn concat() { + assert vec::concat([[1], [2,3]]) == [1, 2, 3]; +} + // Local Variables: // mode: rust; // fill-column: 78;