Search code examples
arraysrustconcatenation

How to concatenate arrays of known lengths?


I have two arrays of known lengths:

let left: [u8; 2] = [1, 2];
let right: [u8; 3] = [3, 4, 5];

My first attempt:

let whole: [u8; 5] = left + right;

fails with the error:

error[E0369]: cannot add `[u8; 2]` to `[u8; 3]`
  --> /home/fadedbee/test.rs:25:29
   |
25 |         let whole: [u8; 5] = left + right;
   |                              ---- ^ ----- [u8; 3]
   |                              |
   |                              [u8; 2]

Likewise:

let whole: [u8; 5] = left.concat(right);

fails with:

error[E0599]: the method `concat` exists for array `[u8; 2]`, but its trait bounds were not satisfied
  --> /home/fadedbee/test.rs:25:29
   |
25 |         let whole: [u8; 5] = left.concat(right);
   |                                   ^^^^^^ method cannot be called on `[u8; 2]` due to unsatisfied trait bounds
   |
   = note: the following trait bounds were not satisfied:
           `<[u8] as std::slice::Concat<_>>::Output = _`

I'm currently using an expression of the form:

let whole: [u8; 5] = [left[0], left[1], right[0], right[1], right[2]];

but this is dozens of elements for my actual use-case and is prone to typos.

@Emoun kindly pointed out that I'd misused concat.

Trying it properly:

 let whole: [u8; 5] = [left, right].concat();

I get:

error[E0308]: mismatched types
  --> /home/fadedbee/test.rs:32:31
   |
32 |         let whole: [u8; 5] = [left, right].concat();
   |                                     ^^^^^ expected an array with a fixed size of 2 elements, found one with 3 elements
   |
   = note: expected type `[u8; 2]`
             found array `[u8; 3]`

How do I concatenate arrays of known lengths into a fixed length array?


Solution

  • I guess there is a better answer, but you can do like this:

    fn main() {
        let left: [u8; 2] = [1, 2];
        let right: [u8; 3] = [3, 4, 5];
    
        let whole: [u8; 5] = {
            let mut whole: [u8; 5] = [0; 5];
            let (one, two) = whole.split_at_mut(left.len());
            one.copy_from_slice(&left);
            two.copy_from_slice(&right);
            whole
        };
    
        println!("{:?}", whole);
    }