]> git.lizzy.rs Git - rust.git/blob - src/libcollections/macros.rs
Rollup merge of #31295 - steveklabnik:gh31266, r=alexcrichton
[rust.git] / src / libcollections / macros.rs
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.
4 //
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.
10
11 /// Creates a `Vec` containing the arguments.
12 ///
13 /// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
14 /// There are two forms of this macro:
15 ///
16 /// - Create a `Vec` containing a given list of elements:
17 ///
18 /// ```
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);
23 /// ```
24 ///
25 /// - Create a `Vec` from a given element and size:
26 ///
27 /// ```
28 /// let v = vec![1; 3];
29 /// assert_eq!(v, [1, 1, 1]);
30 /// ```
31 ///
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
34 /// a constant.
35 ///
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
40 /// boxed integers.
41 #[cfg(not(test))]
42 #[macro_export]
43 #[stable(feature = "rust1", since = "1.0.0")]
44 macro_rules! vec {
45     ($elem:expr; $n:expr) => (
46         $crate::vec::from_elem($elem, $n)
47     );
48     ($($x:expr),*) => (
49         <[_]>::into_vec($crate::boxed::Box::new([$($x),*]))
50     );
51     ($($x:expr,)*) => (vec![$($x),*])
52 }
53
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
58 #[cfg(test)]
59 macro_rules! vec {
60     ($elem:expr; $n:expr) => (
61         $crate::vec::from_elem($elem, $n)
62     );
63     ($($x:expr),*) => (
64         $crate::slice::into_vec($crate::boxed::Box::new([$($x),*]))
65     );
66     ($($x:expr,)*) => (vec![$($x),*])
67 }
68
69 /// Use the syntax described in `std::fmt` to create a value of type `String`.
70 /// See `std::fmt` for more information.
71 ///
72 /// # Examples
73 ///
74 /// ```
75 /// format!("test");
76 /// format!("hello {}", "world!");
77 /// format!("x = {}, y = {y}", 10, y = 30);
78 /// ```
79 #[macro_export]
80 #[stable(feature = "rust1", since = "1.0.0")]
81 macro_rules! format {
82     ($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*)))
83 }