]> git.lizzy.rs Git - rust.git/blob - library/alloc/src/macros.rs
Auto merge of #77502 - varkor:const-generics-suggest-enclosing-braces, r=petrochenkov
[rust.git] / library / alloc / src / macros.rs
1 /// Creates a [`Vec`] containing the arguments.
2 ///
3 /// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
4 /// There are two forms of this macro:
5 ///
6 /// - Create a [`Vec`] containing a given list of elements:
7 ///
8 /// ```
9 /// let v = vec![1, 2, 3];
10 /// assert_eq!(v[0], 1);
11 /// assert_eq!(v[1], 2);
12 /// assert_eq!(v[2], 3);
13 /// ```
14 ///
15 /// - Create a [`Vec`] from a given element and size:
16 ///
17 /// ```
18 /// let v = vec![1; 3];
19 /// assert_eq!(v, [1, 1, 1]);
20 /// ```
21 ///
22 /// Note that unlike array expressions this syntax supports all elements
23 /// which implement [`Clone`] and the number of elements doesn't have to be
24 /// a constant.
25 ///
26 /// This will use `clone` to duplicate an expression, so one should be careful
27 /// using this with types having a nonstandard `Clone` implementation. For
28 /// example, `vec![Rc::new(1); 5]` will create a vector of five references
29 /// to the same boxed integer value, not five references pointing to independently
30 /// boxed integers.
31 ///
32 /// [`Vec`]: crate::vec::Vec
33 #[cfg(not(test))]
34 #[macro_export]
35 #[stable(feature = "rust1", since = "1.0.0")]
36 #[allow_internal_unstable(box_syntax)]
37 macro_rules! vec {
38     () => (
39         $crate::vec::Vec::new()
40     );
41     ($elem:expr; $n:expr) => (
42         $crate::vec::from_elem($elem, $n)
43     );
44     ($($x:expr),+ $(,)?) => (
45         <[_]>::into_vec(box [$($x),+])
46     );
47 }
48
49 // HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is
50 // required for this macro definition, is not available. Instead use the
51 // `slice::into_vec`  function which is only available with cfg(test)
52 // NB see the slice::hack module in slice.rs for more information
53 #[cfg(test)]
54 macro_rules! vec {
55     () => (
56         $crate::vec::Vec::new()
57     );
58     ($elem:expr; $n:expr) => (
59         $crate::vec::from_elem($elem, $n)
60     );
61     ($($x:expr),*) => (
62         $crate::slice::into_vec(box [$($x),*])
63     );
64     ($($x:expr,)*) => (vec![$($x),*])
65 }
66
67 /// Creates a `String` using interpolation of runtime expressions.
68 ///
69 /// The first argument `format!` receives is a format string. This must be a string
70 /// literal. The power of the formatting string is in the `{}`s contained.
71 ///
72 /// Additional parameters passed to `format!` replace the `{}`s within the
73 /// formatting string in the order given unless named or positional parameters
74 /// are used; see [`std::fmt`][fmt] for more information.
75 ///
76 /// A common use for `format!` is concatenation and interpolation of strings.
77 /// The same convention is used with [`print!`] and [`write!`] macros,
78 /// depending on the intended destination of the string.
79 ///
80 /// To convert a single value to a string, use the [`to_string`] method. This
81 /// will use the [`Display`] formatting trait.
82 ///
83 /// [fmt]: core::fmt
84 /// [`print!`]: ../std/macro.print.html
85 /// [`write!`]: core::write
86 /// [`to_string`]: crate::string::ToString
87 /// [`Display`]: core::fmt::Display
88 ///
89 /// # Panics
90 ///
91 /// `format!` panics if a formatting trait implementation returns an error.
92 /// This indicates an incorrect implementation
93 /// since `fmt::Write for String` never returns an error itself.
94 ///
95 /// # Examples
96 ///
97 /// ```
98 /// format!("test");
99 /// format!("hello {}", "world!");
100 /// format!("x = {}, y = {y}", 10, y = 30);
101 /// ```
102 #[macro_export]
103 #[stable(feature = "rust1", since = "1.0.0")]
104 macro_rules! format {
105     ($($arg:tt)*) => {{
106         let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
107         res
108     }}
109 }