]> git.lizzy.rs Git - rust.git/commitdiff
libcore: add vec push.
authorErick Tryzelaar <erick.tryzelaar@gmail.com>
Mon, 19 Dec 2011 17:48:30 +0000 (09:48 -0800)
committerErick Tryzelaar <erick.tryzelaar@gmail.com>
Mon, 19 Dec 2011 18:22:07 +0000 (10:22 -0800)
This is a simple wrapper around grow for the common
case of pushing a value on the end of a vector.

src/libcore/vec.rs
src/test/stdtest/vec.rs

index 91621b8991d24fe0465562929ca42368d908da66..90e1ff3786491601cc12629df970a6e66030c0b8 100644 (file)
@@ -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.
 
 
index 4d53f1b3aa74576fd61623d30bf9699afa8b58fd..9994463ec3c9107ed2bb104900cd930565376cda 100644 (file)
@@ -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().