Fast Cloning of Partial Slices in Rust
TL;DR
When you want to copy an array in Rust, what is actually the fastest approach?
The short answer is: just use clone_from_slice.
Reading the Implementation of clone_from_slice
clone_from_slice
The implementation is shown below. It simply delegates to spec_clone_from.
spec_clone_from
So what exactly is spec_clone_from? It is a function that performs something like overloading based on T. This kind of overloading-like behavior can be achieved using traits (reference).
When T: Clone, it simply calls clone on each element. When T: Copy, it calls copy_from_slice instead.
copy_from_slice
This calls ptr::copy_nonoverlapping. The necessary safety checks are also performed. Since ptr::copy_nonoverlapping is essentially memcpy, the Copy trait is required.
Conclusion
So, calling clone_from_slice will handle everything appropriately for you.
If you want to be explicit, you could use ptr::copy_nonoverlapping directly, but since it is best to avoid unsafe when possible, calling copy_from_slice is the recommended approach.