We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3246723 commit 5cf99e0Copy full SHA for 5cf99e0
src/libcore/vec.rs
@@ -396,6 +396,15 @@ fn shift<T>(&v: [T]) -> T {
396
}
397
398
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
+
408
#[doc = "Remove the last element from a vector and return it"]
409
fn pop<T>(&v: [const T]) -> T unsafe {
410
let ln = len(v);
@@ -2197,6 +2206,13 @@ mod tests {
2197
2206
assert addr == addr_imm;
2198
2207
2199
2208
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
2200
2216
#[test]
2201
2217
fn test_capacity() {
2202
2218
let mut v = [0u64];
0 commit comments