This crate defines concat_arrays!
, a macro that allows you to concatenate arrays.
use concat_arrays::concat_arrays;
fn main() {
let x = [0];
let y = [1, 2];
let z = [3, 4, 5];
let concatenated = concat_arrays!(x, y, z);
assert_eq!(concatenated, [0, 1, 2, 3, 4, 5]);
}
Due to limitations in rust concat_arrays!
can't tell the compiler what the
length of the returned array is. As such, the length needs to be inferable
from the surrounding context. For example, in the example above the length is
inferred by the call to assert_eq!
. It is safe to mis-specify the length
however since you'll get a compilation error rather than broken code.
Inspiration for how to implement this was taken largely from the
const-concat
crate (which
implements compile-time array concatenation).