fork download
  1. struct Fibonacci {t: (u64, u64)}
  2. impl Iterator for Fibonacci {
  3. type Item = u64;
  4. fn next(&mut self) -> Option<Self::Item> {
  5. let current = Some(self.t.0);
  6. self.t = (self.t.1, self.t.0 + self.t.1);
  7. current
  8. }
  9. }
  10. fn main() {
  11. let fib = |a, b| Fibonacci {t: (a, b)};
  12. let f = |a, b, c| fib(a, b).take_while(|&x| x <= c).position(|x| x == c);
  13. for c in 1..6 {println!("c: {}, pos: {:?}", c, f(1, 1, c))}
  14. }
  15.  
Success #stdin #stdout 0s 14864KB
stdin
Standard input is empty
stdout
c: 1, pos: Some(0)
c: 2, pos: Some(2)
c: 3, pos: Some(3)
c: 4, pos: None
c: 5, pos: Some(4)