I want to write the end of a slice to the top of the same slice.
let mut foo = [1, 2, 3, 4, 5];
foo[..2].copy_from_slice(&[4..]); // error: multiple references to same data (mut and not)
assert!(foo, [4, 5, 3, 4, 5]);
I've seen How to operate on 2 mutable slices of a Rust array
I want the maximum performance possible (for example, by using foo.as_ptr()
).
To copy data from one range inside a slice to another in general (allowing overlap), we can't even use
.split_at_mut()
.I would use
.split_at_mut()
primarily otherwise. (Is there anything that makes you think the bounds check is not going to be optimized out? Also, are you copying enough data that it's a small effect in comparison?)Anyway, this is how you could wrap
std::ptr::copy
(overlap-allowing copy, a.k.a memmove) in a safe or anunsafe
function.Playground link
I found a better way to do what I want:
If your types implement
Copy
and the subslices are not overlapping: