2017-08-29 10:17:33 -07:00
|
|
|
/// Creates a [`Vec`] containing the arguments.
|
2017-06-13 15:52:59 -07:00
|
|
|
///
|
|
|
|
/// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
|
|
|
|
/// There are two forms of this macro:
|
|
|
|
///
|
2017-08-29 10:17:33 -07:00
|
|
|
/// - Create a [`Vec`] containing a given list of elements:
|
2017-06-13 15:52:59 -07:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let v = vec![1, 2, 3];
|
|
|
|
/// assert_eq!(v[0], 1);
|
|
|
|
/// assert_eq!(v[1], 2);
|
|
|
|
/// assert_eq!(v[2], 3);
|
|
|
|
/// ```
|
|
|
|
///
|
2017-08-29 10:17:33 -07:00
|
|
|
/// - Create a [`Vec`] from a given element and size:
|
2017-06-13 15:52:59 -07:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let v = vec![1; 3];
|
|
|
|
/// assert_eq!(v, [1, 1, 1]);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Note that unlike array expressions this syntax supports all elements
|
2017-08-29 10:17:33 -07:00
|
|
|
/// which implement [`Clone`] and the number of elements doesn't have to be
|
2017-06-13 15:52:59 -07:00
|
|
|
/// a constant.
|
|
|
|
///
|
2017-08-29 10:17:33 -07:00
|
|
|
/// This will use `clone` to duplicate an expression, so one should be careful
|
2017-06-13 15:52:59 -07:00
|
|
|
/// using this with types having a nonstandard `Clone` implementation. For
|
|
|
|
/// example, `vec![Rc::new(1); 5]` will create a vector of five references
|
|
|
|
/// to the same boxed integer value, not five references pointing to independently
|
|
|
|
/// boxed integers.
|
2017-08-29 10:17:33 -07:00
|
|
|
///
|
2020-08-20 23:43:46 +02:00
|
|
|
/// [`Vec`]: crate::vec::Vec
|
2017-06-13 15:52:59 -07:00
|
|
|
#[cfg(not(test))]
|
|
|
|
#[macro_export]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-02-27 16:58:12 -07:00
|
|
|
#[allow_internal_unstable(box_syntax)]
|
2017-06-13 15:52:59 -07:00
|
|
|
macro_rules! vec {
|
2020-03-31 21:37:13 +02:00
|
|
|
() => (
|
|
|
|
$crate::vec::Vec::new()
|
|
|
|
);
|
2017-06-13 15:52:59 -07:00
|
|
|
($elem:expr; $n:expr) => (
|
|
|
|
$crate::vec::from_elem($elem, $n)
|
|
|
|
);
|
2020-04-14 10:27:55 +03:00
|
|
|
($($x:expr),+ $(,)?) => (
|
|
|
|
<[_]>::into_vec(box [$($x),+])
|
2017-06-13 15:52:59 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is
|
|
|
|
// required for this macro definition, is not available. Instead use the
|
|
|
|
// `slice::into_vec` function which is only available with cfg(test)
|
|
|
|
// NB see the slice::hack module in slice.rs for more information
|
|
|
|
#[cfg(test)]
|
|
|
|
macro_rules! vec {
|
2020-03-31 21:37:13 +02:00
|
|
|
() => (
|
|
|
|
$crate::vec::Vec::new()
|
|
|
|
);
|
2017-06-13 15:52:59 -07:00
|
|
|
($elem:expr; $n:expr) => (
|
|
|
|
$crate::vec::from_elem($elem, $n)
|
|
|
|
);
|
|
|
|
($($x:expr),*) => (
|
|
|
|
$crate::slice::into_vec(box [$($x),*])
|
|
|
|
);
|
|
|
|
($($x:expr,)*) => (vec![$($x),*])
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:17:33 -07:00
|
|
|
/// Creates a `String` using interpolation of runtime expressions.
|
|
|
|
///
|
2019-02-09 21:23:30 +00:00
|
|
|
/// The first argument `format!` receives is a format string. This must be a string
|
|
|
|
/// literal. The power of the formatting string is in the `{}`s contained.
|
2017-08-29 10:17:33 -07:00
|
|
|
///
|
|
|
|
/// Additional parameters passed to `format!` replace the `{}`s within the
|
|
|
|
/// formatting string in the order given unless named or positional parameters
|
2019-02-09 22:16:58 +00:00
|
|
|
/// are used; see [`std::fmt`][fmt] for more information.
|
2017-08-29 10:17:33 -07:00
|
|
|
///
|
|
|
|
/// A common use for `format!` is concatenation and interpolation of strings.
|
|
|
|
/// The same convention is used with [`print!`] and [`write!`] macros,
|
|
|
|
/// depending on the intended destination of the string.
|
2017-06-13 15:52:59 -07:00
|
|
|
///
|
2019-02-09 21:23:30 +00:00
|
|
|
/// To convert a single value to a string, use the [`to_string`] method. This
|
2018-12-29 02:54:05 -05:00
|
|
|
/// will use the [`Display`] formatting trait.
|
|
|
|
///
|
2020-08-20 23:43:46 +02:00
|
|
|
/// [fmt]: core::fmt
|
2017-08-29 16:39:11 -07:00
|
|
|
/// [`print!`]: ../std/macro.print.html
|
2020-08-20 23:43:46 +02:00
|
|
|
/// [`write!`]: core::write
|
|
|
|
/// [`to_string`]: crate::string::ToString
|
|
|
|
/// [`Display`]: core::fmt::Display
|
2017-06-13 15:52:59 -07:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// `format!` panics if a formatting trait implementation returns an error.
|
|
|
|
/// This indicates an incorrect implementation
|
|
|
|
/// since `fmt::Write for String` never returns an error itself.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// format!("test");
|
|
|
|
/// format!("hello {}", "world!");
|
|
|
|
/// format!("x = {}, y = {y}", 10, y = 30);
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
macro_rules! format {
|
2019-09-27 17:15:17 -04:00
|
|
|
($($arg:tt)*) => {{
|
|
|
|
let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
|
|
|
|
res
|
|
|
|
}}
|
2017-06-13 15:52:59 -07:00
|
|
|
}
|