练习:Fibonacci

The first and second Fibonacci numbers are both 1. For n>2, the n’th Fibonacci number is calculated recursively as the sum of the n-1’th and n-2’th Fibonacci numbers.

Write a function fib(n) that calculates the n’th Fibonacci number. When will this function panic?

1. fn fib(n: u32) -> u32 {
2. if n <= 2 {
3. // The base case.
4. todo!("Implement this")
5. } else {
6. // The recursive case.
7. todo!("Implement this")
8. }
9. }

11. fn main() {
12. let n = 20;
13. println!("fib({n}) = {}", fib(n));
14. }