Skip to content

Instantly share code, notes, and snippets.

@dyllandry
Created December 9, 2021 16:20
Show Gist options
  • Select an option

  • Save dyllandry/eb22db457a9d059278169e56f860a32d to your computer and use it in GitHub Desktop.

Select an option

Save dyllandry/eb22db457a9d059278169e56f860a32d to your computer and use it in GitHub Desktop.
Rust Fibonacci
fn main() {
test_fibonacci(10);
}
fn test_fibonacci(n: u32) {
println!("fibonacci({}) -> {}", n, fibonacci(n));
}
fn fibonacci(n: u32) -> u32 {
// Whenever I use recursion, I think of it in terms of what are the
// possible outcomes of each level of recursion. Having a top level
// match statement makes this recursive function for me very easy
// to read.
match n {
0 => return 0,
1 => return 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment