2017-12-26 07:11:18 -06:00
|
|
|
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]
|
2020-10-24 18:21:40 -05:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { (usize::MAX, None) }
|
2017-12-26 07:11:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let a = iterate(0, |x| x+1);
|
|
|
|
println!("{:?}", a.iter().take(10).collect::<Vec<usize>>());
|
2020-01-08 10:05:31 -06:00
|
|
|
//~^ ERROR no method named `iter` found
|
2017-12-26 07:11:18 -06:00
|
|
|
}
|