From 5cf99e02b5f430812e8bce64bcf55c2194ace3ca Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Fri, 22 Jun 2012 16:31:57 -0700 Subject: [PATCH] Adding unshift again. --- src/libcore/vec.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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];