What is the best way to replace a specific portion of a vector with a new vector?
As of now, I am using hardcoded code to replace the vector. What is the most effective way to achieve this?
fn main() {
let mut v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
let u = vec![0,0,0,0];
v[2] = u[0];
v[3] = u[1];
v[4] = u[2];
v[5] = u[3];
println!("v = {:?}", v);
}
Is there any function to replace the vector with given indices?
>Solution :
For Copy types:
v[2..][..u.len()].copy_from_slice(&u);
For non-Copy types:
v.splice(2..2 + u.len(), u);