Hi,
I have the following piece of code
#[derive(Debug, Copy, Clone)]
struct Float(f64);
#[derive(Debug)]
struct Series {
data: Vec<Float>,
}
impl IntoIterator for Series {
type Item = Float;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()
}
}
impl From<&str> for Series {
fn from(s: &str) -> Self {
let mut data = Vec::new();
let vs: Vec<&str> = s.split(" ").collect();
for v in vs.iter() {
let v: f64 = v.parse().unwrap();
data.push(Float(v));
}
Series{data: data}
}
}
fn main() {
let s1 = Series::from("1 2 3.4");
println!("First element => {:?}", s1.into_iter().next());
println!("{:?}", s1);
}
The code doesn't compile for the following reason
move occurs because
s1
has typeSeries
, which does not implement theCopy
trait
Looking at the docs I understand that a type can only implement Copy if all its components can implement copy.
So in my case Vec<T>
cannot implement copy.
Can someone explain to me (rather new to programming)
- why Copy cannot be implemented on
Vec<T>
- how I have to change my code so I could call next and then use the remainder of Series in another place of my code
Thanks for your help!