Skip to content

Instantly share code, notes, and snippets.

@brianseitel
Created January 17, 2014 00:37
Show Gist options
  • Save brianseitel/8466308 to your computer and use it in GitHub Desktop.
Save brianseitel/8466308 to your computer and use it in GitHub Desktop.
Fibonacci implementation in Rust
fn main() {
let max: uint = 25;
let results = fibonacci(max, ~[]);
for i in results.iter() {
println!("{}", i.to_str());
}
}
fn fibonacci(max: uint, mut list: ~[int]) -> ~[int] {
if list.len() == 0 {
list = ~[0,1];
} else if list.len() == max {
return list;
}
// Get the last two items in the list
let f1 = *list.iter().nth(list.len() - 1).unwrap();
let f2 = *list.iter().nth(list.len() - 2).unwrap();
// Add them together, then add that result to the list
list.push(f1 + f2);
// Fibonacci-ize the list again!
return fibonacci(max, list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment