2017-08-29 17:17:33 +00:00
|
|
|
/// Creates a [`Vec`] containing the arguments.
|
2017-06-13 22:52:59 +00: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 17:17:33 +00:00
|
|
|
/// - Create a [`Vec`] containing a given list of elements:
|
2017-06-13 22:52:59 +00: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 17:17:33 +00:00
|
|
|
/// - Create a [`Vec`] from a given element and size:
|
2017-06-13 22:52:59 +00: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 17:17:33 +00:00
|
|
|
/// which implement [`Clone`] and the number of elements doesn't have to be
|
2017-06-13 22:52:59 +00:00
|
|
|
/// a constant.
|
|
|
|
///
|
2017-08-29 17:17:33 +00:00
|
|
|
/// This will use `clone` to duplicate an expression, so one should be careful
|
2017-06-13 22:52:59 +00: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 17:17:33 +00:00
|
|
|
///
|
2020-12-11 15:09:40 +00:00
|
|
|
/// Also, note that `vec![expr; 0]` is allowed, and produces an empty vector.
|
|
|
|
/// This will still evaluate `expr`, however, and immediately drop the resulting value, so
|
|
|
|
/// be mindful of side effects.
|
2020-12-10 22:47:28 +00:00
|
|
|
///
|
2020-08-20 21:43:46 +00:00
|
|
|
/// [`Vec`]: crate::vec::Vec
|
2022-07-01 13:48:23 +00:00
|
|
|
#[cfg(all(not(no_global_oom_handling), not(test)))]
|
2022-05-28 14:37:52 +00:00
|
|
|
#[macro_export]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
#[rustc_diagnostic_item = "vec_macro"]
|
|
|
|
#[allow_internal_unstable(rustc_attrs, liballoc_internals)]
|
|
|
|
macro_rules! vec {
|
|
|
|
() => (
|
2024-06-23 06:12:39 +00:00
|
|
|
$crate::vec::Vec::new()
|
2022-05-28 14:37:52 +00:00
|
|
|
);
|
|
|
|
($elem:expr; $n:expr) => (
|
2024-06-23 06:12:39 +00:00
|
|
|
$crate::vec::from_elem($elem, $n)
|
2022-05-28 14:37:52 +00:00
|
|
|
);
|
|
|
|
($($x:expr),+ $(,)?) => (
|
2024-06-23 06:12:39 +00:00
|
|
|
<[_]>::into_vec(
|
2023-02-26 01:05:27 +00:00
|
|
|
// This rustc_box is not required, but it produces a dramatic improvement in compile
|
|
|
|
// time when constructing arrays with many elements.
|
2022-05-28 14:37:52 +00:00
|
|
|
#[rustc_box]
|
|
|
|
$crate::boxed::Box::new([$($x),+])
|
2024-06-23 06:12:39 +00:00
|
|
|
)
|
2022-05-28 14:37:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-06-13 22:52:59 +00: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
|
`alloc`: make `vec!` unavailable under `no_global_oom_handling`
The `vec!` macro has 3 rules, but two are not usable under
`no_global_oom_handling` builds of the standard library
(even with a zero size):
```rust
let _ = vec![42]; // Error: requires `exchange_malloc` lang_item.
let _ = vec![42; 0]; // Error: cannot find function `from_elem`.
```
Thus those two rules should not be available to begin with.
The remaining one, with an empty matcher, is just a shorthand for
`new()` and may not make as much sense to have alone, since the
idea behind `vec!` is to enable `Vec`s to be defined with the same
syntax as array expressions. Furthermore, the documentation can be
confusing since it shows the other rules.
Thus perhaps it is better and simpler to disable `vec!` entirely
under `no_global_oom_handling` environments, and let users call
`new()` instead:
```rust
let _: Vec<i32> = vec![];
let _: Vec<i32> = Vec::new();
```
Notwithstanding this, a `try_vec!` macro would be useful, such as
the one introduced in https://github.com/rust-lang/rust/pull/95051.
If the shorthand for `new()` is deemed worth keeping on its own,
then it may be interesting to have a separate `vec!` macro with
a single rule and different, simpler documentation.
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2022-04-15 15:06:09 +00:00
|
|
|
#[cfg(all(not(no_global_oom_handling), test))]
|
2022-05-20 12:54:10 +00:00
|
|
|
#[allow(unused_macro_rules)]
|
2017-06-13 22:52:59 +00:00
|
|
|
macro_rules! vec {
|
2020-03-31 19:37:13 +00:00
|
|
|
() => (
|
|
|
|
$crate::vec::Vec::new()
|
|
|
|
);
|
2017-06-13 22:52:59 +00:00
|
|
|
($elem:expr; $n:expr) => (
|
|
|
|
$crate::vec::from_elem($elem, $n)
|
|
|
|
);
|
|
|
|
($($x:expr),*) => (
|
2022-05-28 14:37:52 +00:00
|
|
|
$crate::slice::into_vec($crate::boxed::Box::new([$($x),*]))
|
2017-06-13 22:52:59 +00:00
|
|
|
);
|
|
|
|
($($x:expr,)*) => (vec![$($x),*])
|
|
|
|
}
|
|
|
|
|
2017-08-29 17:17:33 +00: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 17:17:33 +00:00
|
|
|
/// Additional parameters passed to `format!` replace the `{}`s within the
|
|
|
|
/// formatting string in the order given unless named or positional parameters
|
2022-04-28 13:27:57 +00:00
|
|
|
/// are used.
|
|
|
|
///
|
|
|
|
/// See [the formatting syntax documentation in `std::fmt`](../std/fmt/index.html)
|
|
|
|
/// for details.
|
2017-08-29 17:17:33 +00:00
|
|
|
///
|
|
|
|
/// A common use for `format!` is concatenation and interpolation of strings.
|
|
|
|
/// The same convention is used with [`print!`] and [`write!`] macros,
|
2023-09-06 13:11:21 +00:00
|
|
|
/// depending on the intended destination of the string; all these macros internally use [`format_args!`].
|
2017-06-13 22:52:59 +00: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 07:54:05 +00:00
|
|
|
/// will use the [`Display`] formatting trait.
|
|
|
|
///
|
2023-09-06 13:11:21 +00:00
|
|
|
/// To concatenate literals into a `&'static str`, use the [`concat!`] macro.
|
|
|
|
///
|
2017-08-29 23:39:11 +00:00
|
|
|
/// [`print!`]: ../std/macro.print.html
|
2020-08-20 21:43:46 +00:00
|
|
|
/// [`write!`]: core::write
|
2023-09-06 13:11:21 +00:00
|
|
|
/// [`format_args!`]: core::format_args
|
2020-08-20 21:43:46 +00:00
|
|
|
/// [`to_string`]: crate::string::ToString
|
|
|
|
/// [`Display`]: core::fmt::Display
|
2023-09-06 13:11:21 +00:00
|
|
|
/// [`concat!`]: core::concat
|
2017-06-13 22:52:59 +00: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
|
|
|
|
///
|
|
|
|
/// ```
|
2024-07-04 14:47:20 +00:00
|
|
|
/// # #![allow(unused_must_use)]
|
2023-09-06 13:11:21 +00:00
|
|
|
/// format!("test"); // => "test"
|
|
|
|
/// format!("hello {}", "world!"); // => "hello world!"
|
|
|
|
/// format!("x = {}, y = {val}", 10, val = 30); // => "x = 10, y = 30"
|
2022-08-21 19:28:27 +00:00
|
|
|
/// let (x, y) = (1, 2);
|
2023-09-06 13:11:21 +00:00
|
|
|
/// format!("{x} + {y} = 3"); // => "1 + 2 = 3"
|
2017-06-13 22:52:59 +00:00
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2024-07-04 14:47:20 +00:00
|
|
|
#[allow_internal_unstable(hint_must_use, liballoc_internals)]
|
2021-02-14 19:03:13 +00:00
|
|
|
#[cfg_attr(not(test), rustc_diagnostic_item = "format_macro")]
|
2017-06-13 22:52:59 +00:00
|
|
|
macro_rules! format {
|
2024-07-04 14:47:20 +00:00
|
|
|
($($arg:tt)*) => {
|
|
|
|
$crate::__export::must_use({
|
|
|
|
let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
|
|
|
|
res
|
|
|
|
})
|
|
|
|
}
|
2017-06-13 22:52:59 +00:00
|
|
|
}
|