#[derive(Default)]
struct Fibonacci {
pair: (u64, u64),
cnt: u64,
max: u64,
}
impl Fibonacci {
fn new(max: u64) -> Fibonacci {
Fibonacci {
pair: (0, 1),
max: max,
..Default::default()
}
}
fn reset(&mut self) {
self.pair = (0, 1);
self.cnt = 0;
}
}
impl Iterator for Fibonacci {
type Item = u64;
fn next(&mut self) -> Option<u64> {
if self.max < self.cnt {
return None;
}
self.cnt += 1;
let (a, b) = self.pair;
self.pair = (b, a+b);
Some(a)
}
}
/* コンフリクトになる
impl<'a> Iterator for &'a mut Fibonacci {
type Item = u64;
fn next(&mut self) -> Option<u64> {
if self.max < self.cnt {
return None;
}
self.cnt += 1;
let (a, b) = self.pair;
self.pair = (b, a+b);
Some(a)
}
}
*/
fn main() {
let mut fib = Fibonacci::new(10);
for num in &mut fib {
println!("{}", num);
}
fib.reset();
for num in &mut fib {
println!("{}", num);
}
}