Skip to content

Commit 5cf99e0

Browse files
committed
Adding unshift again.
1 parent 3246723 commit 5cf99e0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/libcore/vec.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,15 @@ fn shift<T>(&v: [T]) -> T {
396396
}
397397
}
398398

399+
#[doc = "Prepend an element to the vector"]
400+
fn unshift<T>(&v: [T], +x: T) {
401+
let mut vv = [x];
402+
v <-> vv;
403+
while len(vv) > 0 {
404+
push(v, shift(vv));
405+
}
406+
}
407+
399408
#[doc = "Remove the last element from a vector and return it"]
400409
fn pop<T>(&v: [const T]) -> T unsafe {
401410
let ln = len(v);
@@ -2197,6 +2206,13 @@ mod tests {
21972206
assert addr == addr_imm;
21982207
}
21992208

2209+
#[test]
2210+
fn test_unshift() {
2211+
let mut x = [1, 2, 3];
2212+
unshift(x, 0);
2213+
assert x == [0, 1, 2, 3];
2214+
}
2215+
22002216
#[test]
22012217
fn test_capacity() {
22022218
let mut v = [0u64];

0 commit comments

Comments
 (0)