Skip to content

Instantly share code, notes, and snippets.

@arunlakshman
Last active September 17, 2017 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arunlakshman/59bf326b5e8aabdec7fe55ffb567a02b to your computer and use it in GitHub Desktop.
Save arunlakshman/59bf326b5e8aabdec7fe55ffb567a02b to your computer and use it in GitHub Desktop.
struct Fibonacci {
current: u64,
next: u64,
}
impl Iterator for Fibonacci {
type Item = u64;
fn next(&mut self) -> Option<u64> {
let t = self.current + self.next;
self.current = self.next;
self.next = t;
Some(self.current)
}
}
fn fibonacci() -> Fibonacci {
Fibonacci {
current: 1,
next: 1,
}
}
fn main() {
let mut sum: u64 = 0;
for i in fibonacci().take_while(|&x| x < 4000000).filter(
|&x| x % 2 == 0,
)
{
sum += i;
}
println!("sum: {}", sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment