let mut foo: [u8; 6] = &[0, 0, 0, 0, 0, 0]
let mut bar: &[u8] = &[1, 2, 3]
I want the desired result:
&[1, 2, 3, 0, 0, 0]
The obvious way:
let foo: [u8; 6] = [bar, vec![0; 6 - bar.len()].as_slice()].concat().try_into()...
- It causes a lot of overhead.
- It’s not practical
Perhaps there are some other ways?
>Solution :
Use copy_from_slice:
fn main() {
let mut foo: [u8; 6] = [0, 0, 0, 0, 0, 0];
let mut bar: &[u8] = &[1, 2, 3];
println!("before: {:?}", foo); // [0, 0, 0, 0, 0, 0]
foo[..bar.len()].copy_from_slice(bar);
println!("after: {:?}", foo); // [1, 2, 3, 0, 0, 0]
}