]> git.lizzy.rs Git - rust.git/blob - library/alloc/src/macros.rs
Rollup merge of #86136 - m-ou-se:proc-macro-open-close-span, r=m-ou-se
[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 /// Also, note that `vec![expr; 0]` is allowed, and produces an empty vector.
33 /// This will still evaluate `expr`, however, and immediately drop the resulting value, so
34 /// be mindful of side effects.
35 ///
36 /// [`Vec`]: crate::vec::Vec
37 #[cfg(not(test))]
38 #[doc(alias = "alloc")]
39 #[doc(alias = "malloc")]
40 #[macro_export]
41 #[stable(feature = "rust1", since = "1.0.0")]
42 #[allow_internal_unstable(box_syntax, liballoc_internals)]
43 macro_rules! vec {
44     () => (
45         $crate::__rust_force_expr!($crate::vec::Vec::new())
46     );
47     ($elem:expr; $n:expr) => (
48         $crate::__rust_force_expr!($crate::vec::from_elem($elem, $n))
49     );
50     ($($x:expr),+ $(,)?) => (
51         $crate::__rust_force_expr!(<[_]>::into_vec(box [$($x),+]))
52     );
53 }
54
55 // HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is
56 // required for this macro definition, is not available. Instead use the
57 // `slice::into_vec`  function which is only available with cfg(test)
58 // NB see the slice::hack module in slice.rs for more information
59 #[cfg(test)]
60 macro_rules! vec {
61     () => (
62         $crate::vec::Vec::new()
63     );
64     ($elem:expr; $n:expr) => (
65         $crate::vec::from_elem($elem, $n)
66     );
67     ($($x:expr),*) => (
68         $crate::slice::into_vec(box [$($x),*])
69     );
70     ($($x:expr,)*) => (vec![$($x),*])
71 }
72
73 /// Creates a `String` using interpolation of runtime expressions.
74 ///
75 /// The first argument `format!` receives is a format string. This must be a string
76 /// literal. The power of the formatting string is in the `{}`s contained.
77 ///
78 /// Additional parameters passed to `format!` replace the `{}`s within the
79 /// formatting string in the order given unless named or positional parameters
80 /// are used; see [`std::fmt`] for more information.
81 ///
82 /// A common use for `format!` is concatenation and interpolation of strings.
83 /// The same convention is used with [`print!`] and [`write!`] macros,
84 /// depending on the intended destination of the string.
85 ///
86 /// To convert a single value to a string, use the [`to_string`] method. This
87 /// will use the [`Display`] formatting trait.
88 ///
89 /// [`std::fmt`]: ../std/fmt/index.html
90 /// [`print!`]: ../std/macro.print.html
91 /// [`write!`]: core::write
92 /// [`to_string`]: crate::string::ToString
93 /// [`Display`]: core::fmt::Display
94 ///
95 /// # Panics
96 ///
97 /// `format!` panics if a formatting trait implementation returns an error.
98 /// This indicates an incorrect implementation
99 /// since `fmt::Write for String` never returns an error itself.
100 ///
101 /// # Examples
102 ///
103 /// ```
104 /// format!("test");
105 /// format!("hello {}", "world!");
106 /// format!("x = {}, y = {y}", 10, y = 30);
107 /// ```
108 #[macro_export]
109 #[stable(feature = "rust1", since = "1.0.0")]
110 #[cfg_attr(not(test), rustc_diagnostic_item = "format_macro")]
111 macro_rules! format {
112     ($($arg:tt)*) => {{
113         let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
114         res
115     }}
116 }
117
118 /// Force AST node to an expression to improve diagnostics in pattern position.
119 #[doc(hidden)]
120 #[macro_export]
121 #[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
122 macro_rules! __rust_force_expr {
123     ($e:expr) => {
124         $e
125     };
126 }