Created
December 9, 2021 16:20
-
-
Save dyllandry/eb22db457a9d059278169e56f860a32d to your computer and use it in GitHub Desktop.
Rust Fibonacci
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
| 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