I'm trying to generalize .split from slices to iterators, and I can't seem to get the types right. This is partially because I'm not very experienced with Rust, and partially because the error message is weirdly vague for what I've seen from Rust. I've included what I believe are the relevant snippets, but there's also a trait Splittable and an implementation for it. The errors (I believe the second error is caused by the first, please tell me if that's not correct):
struct Split<I: Iterator, P: Fn(&I::Item) -> bool> {
iter: I,
pred: P,
}
impl<I: Iterator, P: Fn(&I::Item) -> bool> Iterator for Split<I, P> {
type Item = Vec<I::Item>;
fn next(&mut self) -> Option<Self::Item> {
self.iter
.cloned()
.position(|x| (self.pred)(&x))
.map(|i| self.iter.take(i))
}
}
error[E0271]: type mismatch resolving `<I as std::iter::Iterator>::Item == &_`
--> src/main.rs:13:14
|
13 | .cloned()
| ^^^^^^ expected associated type, found reference
|
= note: expected associated type `<I as std::iter::Iterator>::Item`
found reference `&_`
= note: consider constraining the associated type `<I as std::iter::Iterator>::Item` to `&_` or calling a method that returns `<I as std::iter::Iterator>::Item`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
error[E0599]: no method named `position` found for struct `std::iter::Cloned<I>` in the current scope
--> src/main.rs:14:14
|
14 | .position(|x| (self.pred)(&x))
| ^^^^^^^^ method not found in `std::iter::Cloned<I>`
|
= note: the method `position` exists but the following trait bounds were not satisfied:
`<I as std::iter::Iterator>::Item = &_`
which is required by `std::iter::Cloned<I>: std::iter::Iterator`