Question:
I want to copy the values at one slice of an array into another slice.The obvious way (using iteration) is:
The usual way to copy a slice:
arr
is borrowed twice.The only other way to do this that comes to mind is
std::ptr::copy_nonoverlapping
, which is unsafe.What is the best way to copy a slice into its own array (while knowing that source and destination do not overlap)?
Answer:
Usingcopy_within()
:If you’re sure they do not overlap, and you want to exploit that, you can use unsafe code:
If you have better answer, please add a comment about this, thank you!