Skip to content

Instantly share code, notes, and snippets.

@BasixKOR
Created February 25, 2020 13:59
Show Gist options
  • Save BasixKOR/0368daf32a3e33d3ec7f644f9211726f to your computer and use it in GitHub Desktop.
Save BasixKOR/0368daf32a3e33d3ec7f644f9211726f to your computer and use it in GitHub Desktop.
Fibonacci iterator
struct Fibonacci(usize, usize);
impl Iterator for Fibonacci {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
let second = self.1;
self.1 = self.0 + self.1;
self.0 = second;
Some(self.1)
}
}
impl Default for Fibonacci {
fn default() -> Self { Self(0, 1) }
}
fn main() {
Fibonacci::default().take(30).for_each(|x| println!("{}", x))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment