...

/

Solution Review 3: Find nth Fibonacci Number

Solution Review 3: Find nth Fibonacci Number

This lesson gives a detailed review of the challenge in the previous lesson.

We'll cover the following...

Solution:

Press + to interact
fn fibonacci(term: i32) -> i32 {
match term {
0 => 0,
1 => 1,
_ => fibonacci(term-1) + fibonacci(term-2),
}
}
fn main(){
println!("fibonacci(4)={}",fibonacci(4));
}

Explanation

A recursive function, fibonacci, takes a parameter term of type i32 and returns an integer of type i32, i.e., the nth term of the Fibonacci number.

The recursive function ...