Safe version of `ptr::copy` as `slice::copy`

It would be handful to have safe version of ptr::copy. I think that this one:

fn copy<T: Copy>(src: &[T], dst: &mut [T]) {
    assert!(src.len() <= dst.len());
    unsafe { ptr::copy(src.as_ptr(), dst.as_mut_ptr(), src.len()); }
}

Would be perfectly safe and handful.

PS:

I know that we can use loop but sometimes loop wasn’t be optimised out. Also I think that slice::copy(a, b) is more straightforward than:

for (src, dst) in src.iter().zip(&mut dst) {
    *dst = *src
}
1 Like

The discussion is ongoing in this issue Tracking issue for clone_from_slice stabilization

See the thread I started on the same issue for the same reason: Stabilizing basic functions on arrays and slices

1 Like

fwiw, I like the free function idea. Either with Clone or with Copy bound. Copy is nice because then then name is simple to decide… :smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.