Adding unshift again.

This commit is contained in:
Eric Holk 2012-06-22 16:31:57 -07:00
parent 3246723bf7
commit 5cf99e02b5

View File

@ -396,6 +396,15 @@ fn shift<T>(&v: [T]) -> T {
}
}
#[doc = "Prepend an element to the vector"]
fn unshift<T>(&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<T>(&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];