I\'d like to reuse an iterator I made, so as to avoid paying to recreate it from scratch. But iterators don\'t seem to be cloneable and collect mov
You may use closure to get identical iterators:
#[derive(Debug)]
struct MyStruct{
one:Vec,
two:Vec,
three:String
}
fn main() {
let my_string:String = "ABCD1234absd".into();
let my_iter = || my_string.chars();
let my_struct = MyStruct{
one: my_iter().collect(),
two: my_iter().filter(|x| x.is_numeric()).collect(),
three: my_iter().filter(|x| x.is_lowercase()).collect()
};
println!("{:?}", my_struct);
}
See also this Correct way to return an Iterator? question.
Also you may clone iterator (see @Paolo Falabella answer about iterators cloneability):
fn main() {
let v = vec![1,2,3,4,5,6,7,8,9];
let mut i = v.iter().skip(2);
let mut j = i.clone();
println!("{:?}", i.take(3).collect::>());
println!("{:?}", j.filter(|&x| x%2==0).collect::>());
}
Unfortunately I can't tell which way is more effective