I need something like:
fn my_convert<T, U>(v: &Vec<U>)->Vec<T>{
v.iter().map(|&t|t).collect()
}
Of course, I suppose that only vector of builtin numeric types could be passed to the function.
The compiler tells me that I need to give the trait FromIterator for Vec
the trait
std::iter::FromIterator<U>
is not implemented for `std::vec::Vec
then, I added
impl<T, U> std::iter::FromIterator<U> for Vec<T>{
fn from_iter<I>(iter:I)->Self where I: IntoIterator<Item=U>{
Vec::<T>::new() // just for test
}
}
conflicting implementations of trait
std::iter::FromIterator<_>
for typestd::vec::Vec<_>
: note: conflicting implementation in cratealloc
: - impl std::iter::FromIterator for std::vec::Vec;rustc(E0119)
As we all known, in C++, this kind of conversion is straight forward and intutive. I am a beginner of Rust. I don't know how to define a function like above. I even don't known if it is could be achieved in Rust because of the complexity of the trait for generic type.
Thanks!