Consider the code here (playground):
use std::ops::Mul;
/// A vector of numeric types that returns the number
/// and it's double on iteration.
#[derive(Debug)]
struct DoublePairVec<T: Mul> {
numbers: Vec<T>
}
impl<T> Iterator for DoublePairVec<T>
where T: Mul<Output = T> + Copy
{
type Item = (T, T);
fn next(&mut self) -> Option<self::Item> {
match self.numbers.iter().next() {
Some(n) => Some((n, *n * 2 as T)),
_ => None
}
}
}
pub fn main() {
let feed: DoublePairVec<u32> = DoublePairVec{numbers: vec![]};
feed.numbers.push(2);
feed.numbers.push(3);
feed.numbers.push(4);
for pair in feed {
println!("{:?}", pair);
}
}
I am stuck at the following error:
Compiling playground v0.0.1 (/playground)
error[E0412]: cannot find type `Item` in module `self`
--> src/main.rs:15:40
|
15 | fn next(&mut self) -> Option<self::Item> {
| ^^^^ not found in `self`
help: possible candidates are found in other modules, you can import them into scope
|
1 | use chrono::format::Item;
|
1 | use syn::Item;
|
error[E0605]: non-primitive cast: `{integer}` as `T`
I am just stuck with no idea how to proceed. Tried reading more about Associated Types in the book but no leads.