Last active
August 29, 2015 14:10
-
-
Save archer884/3c3669d19fbf35b5f65c to your computer and use it in GitHub Desktop.
Rust fibonacci sequence
This file contains 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
use std::iter::Unfold; | |
fn main() { | |
let fib_seq = Unfold::new((0i64, 1i64), |state| { | |
// closures pretty much always borrow, so *dereference | |
// to get value instead of pointer | |
let (x, y) = *state; | |
let result = Some(x); | |
// again, set *value, not reference | |
*state = (y, x + y); | |
result | |
}); | |
for n in fib_seq.take(10) { | |
println!("{}", n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment