Skip to content

Commit 8e54e74

Browse files
committed
libcore: add vec push.
This is a simple wrapper around grow for the common case of pushing a value on the end of a vector.
1 parent 6b1c60d commit 8e54e74

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/libcore/vec.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,15 @@ fn pop<copy T>(&v: [const T]) -> T {
300300
ret e;
301301
}
302302

303+
/*
304+
Function: push
305+
306+
Append an element to a vector and return it
307+
*/
308+
fn push<copy T>(&v: [T], initval: T) {
309+
grow(v, 1u, initval)
310+
}
311+
303312
// TODO: More.
304313

305314

src/test/stdtest/vec.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ fn test_pop() {
170170
assert (e == 5);
171171
}
172172

173+
#[test]
174+
fn test_push() {
175+
// Test on-stack push().
176+
let v = [];
177+
vec::push(v, 1);
178+
assert (vec::len(v) == 1u);
179+
assert (v[0] == 1);
180+
181+
// Test on-heap push().
182+
vec::push(v, 2);
183+
assert (vec::len(v) == 2u);
184+
assert (v[0] == 1);
185+
assert (v[1] == 2);
186+
}
187+
173188
#[test]
174189
fn test_grow() {
175190
// Test on-stack grow().

0 commit comments

Comments
 (0)