about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMarvin Löbel <loebel.marvin@gmail.com>2015-01-08 21:44:39 +0100
committerMarvin Löbel <loebel.marvin@gmail.com>2015-01-08 21:53:04 +0100
commitc163effc2b4704e3ec9d0011c90a8bd0bab4cb6c (patch)
tree18406d28334422f460ad2e30f505f7bc3a960687
parent2f99a41fe1a27a48e96bc2616ec9faa6de924386 (diff)
Enabled the `vec![]` macro to use the `[a; b]` repeat syntax.
Closes #15587
-rw-r--r--src/libcollections/macros.rs6
-rw-r--r--src/test/run-pass/vec-macro-repeat.rs17
2 files changed, 22 insertions, 1 deletions
diff --git a/src/libcollections/macros.rs b/src/libcollections/macros.rs
index 0ff869d6183..c078db7d46f 100644
--- a/src/libcollections/macros.rs
+++ b/src/libcollections/macros.rs
@@ -1,4 +1,4 @@
-// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -12,6 +12,10 @@
 #[macro_export]
 #[stable]
 macro_rules! vec {
+    ($x:expr; $y:expr) => ({
+        let xs: $crate::boxed::Box<[_]> = $crate::boxed::Box::new([$x; $y]);
+        $crate::slice::SliceExt::into_vec(xs)
+    });
     ($($x:expr),*) => ({
         let xs: $crate::boxed::Box<[_]> = $crate::boxed::Box::new([$($x),*]);
         $crate::slice::SliceExt::into_vec(xs)
diff --git a/src/test/run-pass/vec-macro-repeat.rs b/src/test/run-pass/vec-macro-repeat.rs
new file mode 100644
index 00000000000..9e69ecfce4f
--- /dev/null
+++ b/src/test/run-pass/vec-macro-repeat.rs
@@ -0,0 +1,17 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+
+pub fn main() {
+    assert_eq!(vec![1; 3], vec![1, 1, 1]);
+    assert_eq!(vec![1; 2], vec![1, 1]);
+    assert_eq!(vec![1; 1], vec![1]);
+    assert_eq!(vec![1; 0], vec![]);
+}