Skip to content

Instantly share code, notes, and snippets.

@archer884
Last active August 29, 2015 14:10
Show Gist options
  • Save archer884/3c3669d19fbf35b5f65c to your computer and use it in GitHub Desktop.
Save archer884/3c3669d19fbf35b5f65c to your computer and use it in GitHub Desktop.
Rust fibonacci sequence
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