I\'ve got one piece of Rust code that compiles and one that\'s very similar that does not.
The one that works:
pub fn do_something(_: Box
To be honest, I'm no expert in Rust at all, but my expectation would have been that both of the snippets you show do not compile. That is because, as you pointed out, Iterator
is a trait and not a type and basically you want do_something
to receive any type which implements Iterator
. Maybe there exists a shortcut such that the compiler can transform the signature into a generic if one of the types is a trait which could be why is sometimes works, but then I'm also not familiar with the Rust language specification enough.
Instead of having do_something
take something of type Iterator
(?) make it a generic of type T
where T
is trait bound.
pub fn do_something(_: Box>)
where T: Iterator- + Send {}
fn main() {
let iter = Box::new(Box::new(vec![1.0].into_iter()));
do_something(iter);
}
Playground
Alternatively, you constrain do_something
entirely to std::vec::IntoIter
and only take parameters of that type.
pub fn do_something(_: Box>>) {}
fn main() {
let iter = Box::new(Box::new(vec![1.0].into_iter()));
do_something(iter);
}
Playground