提交 8e54e74b 编写于 作者: E Erick Tryzelaar

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.
上级 6b1c60d3
......@@ -300,6 +300,15 @@ fn pop<copy T>(&v: [const T]) -> T {
ret e;
}
/*
Function: push
Append an element to a vector and return it
*/
fn push<copy T>(&v: [T], initval: T) {
grow(v, 1u, initval)
}
// TODO: More.
......
......@@ -170,6 +170,21 @@ fn test_pop() {
assert (e == 5);
}
#[test]
fn test_push() {
// Test on-stack push().
let v = [];
vec::push(v, 1);
assert (vec::len(v) == 1u);
assert (v[0] == 1);
// Test on-heap push().
vec::push(v, 2);
assert (vec::len(v) == 2u);
assert (v[0] == 1);
assert (v[1] == 2);
}
#[test]
fn test_grow() {
// Test on-stack grow().
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册