Created
February 25, 2020 13:59
-
-
Save BasixKOR/0368daf32a3e33d3ec7f644f9211726f to your computer and use it in GitHub Desktop.
Fibonacci iterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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