diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index a2e7a20bdf2..1b2a50f9b40 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -396,6 +396,15 @@ fn shift(&v: [T]) -> T { } } +#[doc = "Prepend an element to the vector"] +fn unshift(&v: [T], +x: T) { + let mut vv = [x]; + v <-> vv; + while len(vv) > 0 { + push(v, shift(vv)); + } +} + #[doc = "Remove the last element from a vector and return it"] fn pop(&v: [const T]) -> T unsafe { let ln = len(v); @@ -2197,6 +2206,13 @@ mod tests { assert addr == addr_imm; } + #[test] + fn test_unshift() { + let mut x = [1, 2, 3]; + unshift(x, 0); + assert x == [0, 1, 2, 3]; + } + #[test] fn test_capacity() { let mut v = [0u64];