1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 /// Creates a `Vec` containing the arguments.
13 /// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
14 /// There are two forms of this macro:
16 /// - Create a `Vec` containing a given list of elements:
19 /// let v = vec![1, 2, 3];
20 /// assert_eq!(v[0], 1);
21 /// assert_eq!(v[1], 2);
22 /// assert_eq!(v[2], 3);
25 /// - Create a `Vec` from a given element and size:
28 /// let v = vec![1; 3];
29 /// assert_eq!(v, [1, 1, 1]);
32 /// Note that unlike array expressions this syntax supports all elements
33 /// which implement `Clone` and the number of elements doesn't have to be
36 /// This will use `clone()` to duplicate an expression, so one should be careful
37 /// using this with types having a nonstandard `Clone` implementation. For
38 /// example, `vec![Rc::new(1); 5]` will create a vector of five references
39 /// to the same boxed integer value, not five references pointing to independently
43 #[stable(feature = "rust1", since = "1.0.0")]
45 ($elem:expr; $n:expr) => (
46 $crate::vec::from_elem($elem, $n)
49 <[_]>::into_vec($crate::boxed::Box::new([$($x),*]))
51 ($($x:expr,)*) => (vec![$($x),*])
54 // HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is
55 // required for this macro definition, is not available. Instead use the
56 // `slice::into_vec` function which is only available with cfg(test)
57 // NB see the slice::hack module in slice.rs for more information
60 ($elem:expr; $n:expr) => (
61 $crate::vec::from_elem($elem, $n)
64 $crate::slice::into_vec($crate::boxed::Box::new([$($x),*]))
66 ($($x:expr,)*) => (vec![$($x),*])
69 /// Use the syntax described in `std::fmt` to create a value of type `String`.
70 /// See `std::fmt` for more information.
76 /// format!("hello {}", "world!");
77 /// format!("x = {}, y = {y}", 10, y = 30);
80 #[stable(feature = "rust1", since = "1.0.0")]
82 ($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*)))