blob: 472eef77d7956411ddfa93d4ab60d7e291860ef9 [file] [log] [blame]
Cristi Cobzarenco651cf582016-10-15 15:32:141// Copyright 2013-2016 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
Andy Gaugeb9b65492017-08-29 17:17:3311/// Creates a [`Vec`] containing the arguments.
Murartheadda762017-06-13 22:52:5912///
13/// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
14/// There are two forms of this macro:
15///
Andy Gaugeb9b65492017-08-29 17:17:3316/// - Create a [`Vec`] containing a given list of elements:
Murartheadda762017-06-13 22:52:5917///
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///
Andy Gaugeb9b65492017-08-29 17:17:3325/// - Create a [`Vec`] from a given element and size:
Murartheadda762017-06-13 22:52:5926///
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
Andy Gaugeb9b65492017-08-29 17:17:3333/// which implement [`Clone`] and the number of elements doesn't have to be
Murartheadda762017-06-13 22:52:5934/// a constant.
35///
Andy Gaugeb9b65492017-08-29 17:17:3336/// This will use `clone` to duplicate an expression, so one should be careful
Murartheadda762017-06-13 22:52:5937/// 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.
Andy Gaugeb9b65492017-08-29 17:17:3341///
42/// [`Vec`]: ../std/vec/struct.Vec.html
43/// [`Clone`]: ../std/clone/trait.Clone.html
Murartheadda762017-06-13 22:52:5944#[cfg(not(test))]
45#[macro_export]
46#[stable(feature = "rust1", since = "1.0.0")]
47#[allow_internal_unstable]
48macro_rules! vec {
49 ($elem:expr; $n:expr) => (
50 $crate::vec::from_elem($elem, $n)
51 );
52 ($($x:expr),*) => (
53 <[_]>::into_vec(box [$($x),*])
54 );
55 ($($x:expr,)*) => (vec![$($x),*])
56}
57
58// HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is
59// required for this macro definition, is not available. Instead use the
60// `slice::into_vec` function which is only available with cfg(test)
61// NB see the slice::hack module in slice.rs for more information
62#[cfg(test)]
63macro_rules! vec {
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
Andy Gaugeb9b65492017-08-29 17:17:3373/// Creates a `String` using interpolation of runtime expressions.
74///
Martin Lindheece9a572017-11-21 14:33:4575/// The first argument `format!` receives is a format string. This must be a string
Andy Gaugeb9b65492017-08-29 17:17:3376/// 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`][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.
Murartheadda762017-06-13 22:52:5985///
86/// [fmt]: ../std/fmt/index.html
Andy Gauge80d513a2017-08-29 23:39:1187/// [`print!`]: ../std/macro.print.html
88/// [`write!`]: ../std/macro.write.html
Murartheadda762017-06-13 22:52:5989///
90/// # Panics
91///
92/// `format!` panics if a formatting trait implementation returns an error.
93/// This indicates an incorrect implementation
94/// since `fmt::Write for String` never returns an error itself.
95///
96/// # Examples
97///
98/// ```
99/// format!("test");
100/// format!("hello {}", "world!");
101/// format!("x = {}, y = {y}", 10, y = 30);
102/// ```
103#[macro_export]
104#[stable(feature = "rust1", since = "1.0.0")]
105macro_rules! format {
106 ($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*)))
107}