fn foo<TIterator>(items: TIterator)
where TIterator: Iterator<Item = Bar> {
for item in items {
// use item
}
// do other stuff
for item in items {
// use item
}
}
But my code fails to compile because items is moved in the first for loop. The type Bar is Clone if that helps. In particular, Bar is an Rc to the actual item and I want to store copies of the Rcs in each for loop.
I took a look at that and it gave me an idea. It looks like what I really want is:
fn foo<TIterator>(items: TIterator)
where TIterator: IntoIterator<Item = Bar> + Clone {
for item in items.clone() {
// use item
}
// do other stuff
for item in items {
// use item
}
}
If you want to avoid cloning the entire data structure, you can use IntoIterator by reference.
fn foo<'a, I>(items: &'a I)
where &'a I: IntoIterator<Item = &'a Bar>
{
for item in items {
// use item
}
// do other stuff
for item in items {
// use item
}
}
struct Bar;
fn main() {
foo(&vec![Bar])
}