blob: 977c43b71fb713f8d2dd6c819e45e4242b9ee6ab (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
fn iterate<T, F>(initial: T, f: F) -> Iterate<T, F> {
Iterate {
state: initial,
f: f,
}
}
pub struct Iterate<T, F> {
state: T,
f: F
}
impl<T: Clone, F> Iterator for Iterate<T, F> where F: Fn(&T) -> T {
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.state = (self.f)(&self.state);
Some(self.state.clone())
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) { (usize::MAX, None) }
}
fn main() {
let a = iterate(0, |x| x+1);
println!("{:?}", a.iter().take(10).collect::<Vec<usize>>());
//~^ ERROR no method named `iter` found
}
|