Expand description
This crate provides Go style copying / cloning for slices.
This crate is for those times where it is easier to adjust slices based off the number of elements copied, as opposed to determining the amount to copy before adjusting slices and finally copying.
§Examples
We can use copy
for types that implement Copy
.
use slice_copy::copy;
let mut l = b"hello".to_vec();
let r = b"goodbye".to_vec();
let n = copy(&mut l, &r);
assert_eq!(n, 5);
assert_eq!(l, b"goodb");
Similarly, we can use clone
for types that implement Clone
.
use slice_copy::clone;
let mut l = b"foobarbaz".to_vec();
let r = b"biz".to_vec();
let n = clone(&mut l, &r);
assert_eq!(n, 3);
assert_eq!(l, b"bizbarbaz");
Functions§
- clone
- Clones as many
T
as possible fromsrc
intodst
, returning the number ofT
cloned. This function is short form fordst.clone_from_slice(src)
, but accounts for if their lengths are unequal to avoid panics. - copy
- Copies as many
T
as possible fromsrc
intodst
, returning the number ofT
copied. This function is short form fordst.copy_from_slice(src)
, but accounts for if their lengths are unequal to avoid panics.