Concatenating arrays

Yeah, that works:

fn concat_arrays<T, const A: usize, const B: usize, const C: usize>(a: [T; A], b: [T; B]) -> [T; C] {
    assert_eq!(A+B, C);
    let mut iter = a.into_iter().chain(b);
    std::array::from_fn(|_| iter.next().unwrap())
}

fn main() {
    let a: [u8; 3] = [1, 2, 3];
    let b: [u8; 4] = [4, 5, 6, 7];
    let c: [u8; 7] = concat_arrays(a, b);
    assert_eq!(c, [1, 2, 3, 4, 5, 6, 7]);
    // Compiler won't notice this is wrong, but we get a panic:
    //let _: [u8; 8] = concat_arrays(a, b);
}

(Playground)

Very nice! :smiley: Update: Apparently, it's not very efficient, see post below.

Is there any way to make let _: [u8; 8] = concat_arrays(a, b); throw a compile-time error without using the highly experimental generic_const_exprs feature?