From c163effc2b4704e3ec9d0011c90a8bd0bab4cb6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20L=C3=B6bel?= Date: Thu, 8 Jan 2015 21:44:39 +0100 Subject: [PATCH] Enabled the `vec![]` macro to use the `[a; b]` repeat syntax. Closes #15587 --- src/libcollections/macros.rs | 6 +++++- src/test/run-pass/vec-macro-repeat.rs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/test/run-pass/vec-macro-repeat.rs diff --git a/src/libcollections/macros.rs b/src/libcollections/macros.rs index 0ff869d618316..c078db7d46fb7 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 0000000000000..9e69ecfce4fbe --- /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 or the MIT license +// , 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![]); +}