blob: fbc29e4f23322ec4bc56ce2de38afbcbf0693f0c (
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
30
|
use num::{One, Zero};
use std::mem;
use std::ops::AddAssign;
#[derive(Debug)]
pub struct Fib<T> {
a: T,
b: T,
}
impl<T: Zero + One> Fib<T> {
pub fn new() -> Self {
Self {
a: T::zero(),
b: T::one(),
}
}
}
impl<T: Clone + AddAssign> Iterator for Fib<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
let res = self.a.clone();
mem::swap(&mut self.a, &mut self.b);
self.b += self.a.clone();
Some(res)
}
}
|