Macro constcat::concat_slices
source · macro_rules! concat_slices { ([$init:expr; $T:ty]: $($s:expr),+ $(,)?) => { ... }; ([char]: $($s:expr),+ $(,)?) => { ... }; ([f32]: $($s:expr),+ $(,)?) => { ... }; ([f64]: $($s:expr),+ $(,)?) => { ... }; ([$T:ty]: $($s:expr),+ $(,)?) => { ... }; }
Expand description
Concatenate const
&[T]
expressions into a static slice.
- This macro takes any number of comma-separated
&[T]
expressions and yields an expression of type&'static [T]
which represents all of the expressions concatenated left-to-right. - The macro requires that type of slice be specified, e.g.
[usize]
or[u8]
before the comma separate expressions. - You can optionally provide an initializer for non-integer types, e.g.
[0.0; f32]
for floating point numbers,[false; bool]
forbool
s, or['\x00'; char]
forchar
s. This also works for custom types as long as the type and initializer expression is able to be specified in an array initializer expression.
Examples
Basic usage with integers:
const fn more() -> &'static [i32] { &[4, 5, 6] }
const EXAMPLE: &[i32] = concat_slices!([i32]: &[1, 2, 3], more());
assert_eq!(EXAMPLE, [1, 2, 3, 4, 5, 6])
With a constant initializer:
const fn more() -> &'static [f32] { &[4.0, 5.0, 6.0] }
const EXAMPLE: &[f32] = concat_slices!([0.0; f32]: &[1.0, 2.0, 3.0], more());
assert_eq!(EXAMPLE, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0])