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(no_global_oom_handling))] |
38 | #[macro_export] |
39 | #[stable(feature = "rust1", since = "1.0.0")] |
40 | #[rustc_diagnostic_item= "vec_macro"] |
41 | #[allow_internal_unstable(rustc_attrs, liballoc_internals)] |
42 | macro_rules! vec { |
43 | () => ( |
44 | $crate::vec::Vec::new() |
45 | ); |
46 | ($elem:expr; $n:expr) => ( |
47 | $crate::vec::from_elem($elem, $n) |
48 | ); |
49 | ($($x:expr),+ $(,)?) => ( |
50 | <[_]>::into_vec( |
51 | // Using the intrinsic produces a dramatic improvement in stack usage for |
52 | // unoptimized programs using this code path to construct large Vecs. |
53 | $crate::boxed::box_new([$($x),+]) |
54 | ) |
55 | ); |
56 | } |
57 | |
58 | /// Creates a `String` using interpolation of runtime expressions. |
59 | /// |
60 | /// The first argument `format!` receives is a format string. This must be a string |
61 | /// literal. The power of the formatting string is in the `{}`s contained. |
62 | /// Additional parameters passed to `format!` replace the `{}`s within the |
63 | /// formatting string in the order given unless named or positional parameters |
64 | /// are used. |
65 | /// |
66 | /// See [the formatting syntax documentation in `std::fmt`](../std/fmt/index.html) |
67 | /// for details. |
68 | /// |
69 | /// A common use for `format!` is concatenation and interpolation of strings. |
70 | /// The same convention is used with [`print!`] and [`write!`] macros, |
71 | /// depending on the intended destination of the string; all these macros internally use [`format_args!`]. |
72 | /// |
73 | /// To convert a single value to a string, use the [`to_string`] method. This |
74 | /// will use the [`Display`] formatting trait. |
75 | /// |
76 | /// To concatenate literals into a `&'static str`, use the [`concat!`] macro. |
77 | /// |
78 | /// [`print!`]: ../std/macro.print.html |
79 | /// [`write!`]: core::write |
80 | /// [`format_args!`]: core::format_args |
81 | /// [`to_string`]: crate::string::ToString |
82 | /// [`Display`]: core::fmt::Display |
83 | /// [`concat!`]: core::concat |
84 | /// |
85 | /// # Panics |
86 | /// |
87 | /// `format!` panics if a formatting trait implementation returns an error. |
88 | /// This indicates an incorrect implementation |
89 | /// since `fmt::Write for String` never returns an error itself. |
90 | /// |
91 | /// # Examples |
92 | /// |
93 | /// ``` |
94 | /// # #![allow(unused_must_use)] |
95 | /// format!("test"); // => "test" |
96 | /// format!("hello {}", "world!"); // => "hello world!" |
97 | /// format!("x = {}, y = {val}", 10, val = 30); // => "x = 10, y = 30" |
98 | /// let (x, y) = (1, 2); |
99 | /// format!("{x} + {y} = 3"); // => "1 + 2 = 3" |
100 | /// ``` |
101 | #[macro_export] |
102 | #[stable(feature = "rust1", since = "1.0.0")] |
103 | #[allow_internal_unstable(hint_must_use, liballoc_internals)] |
104 | #[rustc_diagnostic_item= "format_macro"] |
105 | macro_rules! format { |
106 | ($($arg:tt)*) => { |
107 | $crate::__export::must_use({ |
108 | $crate::fmt::format($crate::__export::format_args!($($arg)*)) |
109 | }) |
110 | } |
111 | } |
112 |
Definitions
Learn Rust with the experts
Find out more