|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
| 11 | +#[cfg(stage0)] |
11 | 12 | /// Creates a `Vec` containing the arguments.
|
12 | 13 | ///
|
13 | 14 | /// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
|
@@ -45,6 +46,43 @@ macro_rules! vec {
|
45 | 46 | ($($x:expr,)*) => (vec![$($x),*])
|
46 | 47 | }
|
47 | 48 |
|
| 49 | +#[cfg(not(stage0))] |
| 50 | +/// Creates a `Vec` containing the arguments. |
| 51 | +/// |
| 52 | +/// `vec!` allows `Vec`s to be defined with the same syntax as array expressions. |
| 53 | +/// There are two forms of this macro: |
| 54 | +/// |
| 55 | +/// - Create a `Vec` containing a given list of elements: |
| 56 | +/// |
| 57 | +/// ``` |
| 58 | +/// let v = vec![1, 2, 3]; |
| 59 | +/// assert_eq!(v[0], 1); |
| 60 | +/// assert_eq!(v[1], 2); |
| 61 | +/// assert_eq!(v[2], 3); |
| 62 | +/// ``` |
| 63 | +/// |
| 64 | +/// - Create a `Vec` from a given element and size: |
| 65 | +/// |
| 66 | +/// ``` |
| 67 | +/// let v = vec![1; 3]; |
| 68 | +/// assert_eq!(v, [1, 1, 1]); |
| 69 | +/// ``` |
| 70 | +/// |
| 71 | +/// Note that unlike array expressions this syntax supports all elements |
| 72 | +/// which implement `Clone` and the number of elements doesn't have to be |
| 73 | +/// a constant. |
| 74 | +#[macro_export] |
| 75 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 76 | +macro_rules! vec { |
| 77 | + ($elem:expr; $n:expr) => ( |
| 78 | + $crate::vec::from_elem($elem, $n) |
| 79 | + ); |
| 80 | + ($($x:expr),*) => ( |
| 81 | + <[_]>::into_vec($crate::boxed::Box::new([$($x),*])) |
| 82 | + ); |
| 83 | + ($($x:expr,)*) => (vec![$($x),*]) |
| 84 | +} |
| 85 | + |
48 | 86 | /// Use the syntax described in `std::fmt` to create a value of type `String`.
|
49 | 87 | /// See `std::fmt` for more information.
|
50 | 88 | ///
|
|
0 commit comments