Hi,
I am currently learning Rust and playing with vectors. For fun I wanted to write a function that turns a slice into a vector.
Here is one that is working:
pub fn to_vec_from_slice_with_clone<T: Clone>(s: &[T]) -> Vec<T> {
let mut v: Vec<T> = Vec::with_capacity(s.len());
for i in s.iter() {
let j = i.clone();
v.push(j);
}
v
}
This one does not work:
pub fn to_vec_from_slice<T>(s: &[T]) -> Vec<T> {
let mut v: Vec<T> = Vec::with_capacity(s.len());
for i in s.iter() {
v.push(*i);
}
v
}
Compiler says:
error: cannot move out of borrowed content
src/vecutils.rs:18 v.push(*i);
Makes sense to me.
But why does this work?
pub fn to_vec_from_int_slice(s: &[i32]) -> Vec<i32> {
let mut v: Vec<i32> = Vec::with_capacity(s.len());
for i in s.iter() {
v.push(*i);
}
v
}
Same function but i32 instead of T.
It compiles and a test showed that it is working.
What is the difference here? Why does T only work with cloning but i32 right away?
Markus