If your source of data is Vec (or, generally, something that is convertible to slice, i.e. something that is already in memory), you don't need io::copy, you need [T]::copy_from_slice.
Note, however, that this will not work if the memory for target is not initialized. If you want to initialize one Vec with copies of another one (that's what your C code is seemingly intended to do - it's UB as written, but this is another story), it would be better to create id directly, by collecting, like this:
fn main() {
let src = b"abcd1234";
let dest: Vec<u8> = src.iter().copied().cycle().take(1024).collect();
println!("{:?}", dest);
}
From the code, the implementation of this ( clone_from_slice) looks very inefficient.
It just loops and copies.
#[stable(feature = "clone_from_slice", since = "1.7.0")]
pub fn clone_from_slice(&mut self, src: &[T])
where
T: Clone,
{
assert!(self.len() == src.len(), "destination and source slices have different lengths");
// NOTE: We need to explicitly slice them to the same length
// for bounds checking to be elided, and the optimizer will
// generate memcpy for simple cases (for example T = u8).
let len = self.len();
let src = &src[..len];
for i in 0..len {
self[i].clone_from(&src[i]);
}
}
Yes but. According to the comment in that code there will be no bounds checking and at least for byte arrays it will compile down to a memcpy(). So it will be as efficient as C. Which likely compiles to vector instructions or something cunning. Which is about the best one can get.