Skip to content

Commit 2e0593d

Browse files
committed
stdlib: Add vec::concat to concatenate a vector of vectors
Compare to str::concat
1 parent da064ef commit 2e0593d

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/lib/vec.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,19 @@ fn filter<T>(f: block(T) -> bool, v: [mutable? T]) -> [T] {
446446
ret result;
447447
}
448448

449+
/*
450+
Function: concat
451+
452+
Concatenate a vector of vectors. Flattens a vector of vectors of T into
453+
a single vector of T.
454+
*/
455+
fn concat<T>(v: [mutable? [mutable? T]]) -> [T] {
456+
// FIXME: So much copying
457+
let new: [T] = [];
458+
for inner: [T] in v { new += inner; }
459+
ret new;
460+
}
461+
449462
/*
450463
Function: foldl
451464

src/test/stdtest/vec.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@ fn init_empty() {
469469
#[ignore]
470470
fn init_empty() { }
471471

472+
#[test]
473+
fn concat() {
474+
assert vec::concat([[1], [2,3]]) == [1, 2, 3];
475+
}
476+
472477
// Local Variables:
473478
// mode: rust;
474479
// fill-column: 78;

0 commit comments

Comments
 (0)