Is there a better (faster) way to overwrite a part of a Vec with the content of a smaller slice? This is the solution I came up all by myself, leveraging on a previous question I asked here on this forum:
#[derive(Debug)]
struct Stuff {
buffer: Vec<f32>
}
impl Stuff {
fn copy_into(&mut self, position: usize, data: &[f32]) {
for (dest, &src) in self.buffer[position..].iter_mut().zip(data.iter()) {
*dest = src;
}
}
}
fn main() {
let mut s = Stuff { buffer: vec![0.0_f32; 10] };
let data = vec![5.5_f32; 3];
s.copy_into(5, &data);
println!("{s:?}");
}
The sizes of the source and destination vectors are actually going to be a lot bigger that in this example.
In the end, what I need is just a copy of a chunk of memory. Is the compiler able to do it efficiently? If not, what can I do instead?
If you're overwriting something with the same length, then copy_from_slice is absolutely the way to go.
But if you need to replace a sub-range with something that might be longer or shorter, then Vec::splice is the magic wand that can do all kinds of stuff.
Maybe it should be noted that Vec::splice only works on Vecs (because it needs to be able to grow the Vec, for example), while copy_from_slice only requires a &mut[_].