Can trait objects of type IntoIterator be boxed and held inside of a structure? I've a situation where I'd like to store a vector of objects that can be turned into iterators. My attempt at this was the code
struct Foo {
foo: Vec<Box<dyn IntoIterator<Item = usize>>>,
}
fn main() {
let _ = Foo {
foo: vec![Box::new(1..2), Box::new(vec![2,4,4])],
};
}
However, this does not compile and produces the error:
error[E0191]: the value of the associated type `IntoIter` (from trait `IntoIterator`) must be specified
--> src/main.rs:6:22
|
6 | foo: Vec<Box<dyn IntoIterator<Item = usize>>>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: specify the associated type: `IntoIterator<Item = usize, IntoIter = Type>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0191`.
I am aware that most of the time when we want to use IntoIterator we instead pass a generic into the structure. However, in this case, the items that I'd like to store do not have the same time. As such, is there a way to store a heterogenous collection of objects that can all be turned into an iterator of the same type?