rune_alloc/
public_macros.rs

1#[macro_export]
2macro_rules! try_vec {
3    () => (
4        $crate::vec::Vec::new()
5    );
6
7    ($elem:expr; $n:expr) => (
8        $crate::vec::try_from_elem($elem, $n)?
9    );
10
11    ($($x:expr),+ $(,)?) => (
12        $crate::slice::into_vec(
13            // This rustc_box is not required, but it produces a dramatic improvement in compile
14            // time when constructing arrays with many elements.
15            $crate::boxed::Box::try_from([$($x),+])?
16        )
17    );
18}
19
20/// Creates a `String` using interpolation of runtime expressions.
21///
22/// The first argument `try_format!` receives is a format string. This must be a
23/// string literal. The power of the formatting string is in the `{}`s
24/// contained.
25///
26/// Additional parameters passed to `try_format!` replace the `{}`s within the
27/// formatting string in the order given unless named or positional parameters
28/// are used; see [`std::fmt`] for more information.
29///
30/// A common use for `try_format!` is concatenation and interpolation of
31/// strings. The same convention is used with [`print!`] and [`write!`] macros,
32/// depending on the intended destination of the string.
33///
34/// To convert a single value to a string, use the [`try_to_string`] method.
35/// This will use the [`Display`] formatting trait.
36///
37/// [`std::fmt`]: ../std/fmt/index.html
38/// [`print!`]: ../std/macro.print.html
39/// [`write!`]: core::write
40/// [`try_to_string`]: crate::string::TryToString
41/// [`Display`]: core::fmt::Display
42///
43/// # Panics
44///
45/// `try_format!` panics if a formatting trait implementation returns an error. This
46/// indicates an incorrect implementation since `fmt::Write for String` never
47/// returns an error itself.
48///
49/// # Examples
50///
51/// ```
52/// use rune::alloc::try_format;
53///
54/// try_format!("test");
55/// try_format!("hello {}", "world!");
56/// try_format!("x = {}, y = {y}", 10, y = 30);
57/// let (x, y) = (1, 2);
58/// try_format!("{x} + {y} = 3");
59/// # Ok::<_, rune::alloc::Error>(())
60/// ```
61#[macro_export]
62macro_rules! try_format {
63    ($($tt:tt)*) => {
64        $crate::fmt::try_format(format_args!($($tt)*))?
65    };
66}