1 /// Creates a [`Vec`] containing the arguments.
3 /// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
4 /// There are two forms of this macro:
6 /// - Create a [`Vec`] containing a given list of elements:
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);
15 /// - Create a [`Vec`] from a given element and size:
18 /// let v = vec![1; 3];
19 /// assert_eq!(v, [1, 1, 1]);
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
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
32 /// [`Vec`]: ../std/vec/struct.Vec.html
33 /// [`Clone`]: ../std/clone/trait.Clone.html
36 #[stable(feature = "rust1", since = "1.0.0")]
37 #[cfg_attr(not(stage0), allow_internal_unstable(box_syntax))]
38 #[cfg_attr(stage0, allow_internal_unstable)]
40 ($elem:expr; $n:expr) => (
41 $crate::vec::from_elem($elem, $n)
44 <[_]>::into_vec(box [$($x),*])
46 ($($x:expr,)*) => (vec![$($x),*])
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
55 ($elem:expr; $n:expr) => (
56 $crate::vec::from_elem($elem, $n)
59 $crate::slice::into_vec(box [$($x),*])
61 ($($x:expr,)*) => (vec![$($x),*])
64 /// Creates a `String` using interpolation of runtime expressions.
66 /// The first argument `format!` receives is a format string. This must be a string
67 /// literal. The power of the formatting string is in the `{}`s contained.
69 /// Additional parameters passed to `format!` replace the `{}`s within the
70 /// formatting string in the order given unless named or positional parameters
71 /// are used; see [`std::fmt`][fmt] for more information.
73 /// A common use for `format!` is concatenation and interpolation of strings.
74 /// The same convention is used with [`print!`] and [`write!`] macros,
75 /// depending on the intended destination of the string.
77 /// To convert a single value to a string, use the [`to_string`] method. This
78 /// will use the [`Display`] formatting trait.
80 /// [fmt]: ../std/fmt/index.html
81 /// [`print!`]: ../std/macro.print.html
82 /// [`write!`]: ../std/macro.write.html
83 /// [`to_string`]: ../std/string/trait.ToString.html
84 /// [`Display`]: ../std/fmt/trait.Display.html
88 /// `format!` panics if a formatting trait implementation returns an error.
89 /// This indicates an incorrect implementation
90 /// since `fmt::Write for String` never returns an error itself.
96 /// format!("hello {}", "world!");
97 /// format!("x = {}, y = {y}", 10, y = 30);
100 #[stable(feature = "rust1", since = "1.0.0")]
101 macro_rules! format {
102 ($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*)))