Merge 2 arrays into bigger array

I want to merge 2 arrays together and I was wondering if there was any better way than just iterating over them and copying them. In the end, I want to simply merge those 2 arrays and end up with a bigger one containing all of their values in order.

// Based on your original code
fn main() {
    let mut first = [0_u8; 10];
    let second = [1; 4];
    first[0..second.len()].copy_from_slice(&second);
    println!("{first:?}");
}

Documentation.

Note that in Rust, indexing is 0-based.

Appending would be something different and you'd probably want a Vec.

1 Like

Thanks for the incredibly fast answer. I kindof went and used a vec to do the job but this inspired me to check a theory of just copying 2 arrays into a bigger one. I wonder if that would 1. work and 2. be faster than using a vec. Again, thank you for the help.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.