core: Add vec::unshift

This commit is contained in:
Brian Anderson 2012-03-18 16:16:47 -07:00
parent 397f33fd35
commit e4af1ca065

View file

@ -323,6 +323,11 @@ fn shift<T: copy>(&v: [const T]) -> T {
ret e; ret e;
} }
#[doc = "Prepend an element to a vector"]
fn unshift<T: copy>(&v: [const T], +t: T) {
v = [const t] + v;
}
#[doc = "Remove the last element from a vector and return it"] #[doc = "Remove the last element from a vector and return it"]
fn pop<T>(&v: [const T]) -> T unsafe { fn pop<T>(&v: [const T]) -> T unsafe {
let ln = len(v); let ln = len(v);
@ -1734,6 +1739,13 @@ mod tests {
let addr_imm = unsafe::to_ptr(x_imm); let addr_imm = unsafe::to_ptr(x_imm);
assert addr == addr_imm; assert addr == addr_imm;
} }
#[test]
fn test_unshift() {
let x = [1, 2, 3];
unshift(x, 0);
assert x == [0, 1, 2, 3];
}
} }
// Local Variables: // Local Variables: