I have a Vec<T>
where T: Copy + Clone
and I would like to efficiently copy a slice of the vector to another slice of the same vector, where slices have the same length and do not overlap. In C++ I would use std::memcpy
for the same purpose.
I would like to:
- avoid using unsafe functions like
std::ptr::copy_nonoverlapping
- use a library function, without implementing the copy loop myself
What I have tried:
#[test]
fn copy_within_a_vector() {
let mut data = vec![1, 2, 0, 0];
let src = &data[0..2];
let mut dst = &mut data[3..4];
dst.copy_from_slice(src);
}
This code does not even get compiled because the borrow checker complains about borrowing data
both mutable and immutable at the same time (and it is right).
Is it possible to rewrite the code to compile on stable Rust?
Related questions: