Exercise: Fibonacci

The Fibonacci sequence begins with [0,1]. For n>1, 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?

fn fib(n: u32) -> u32 {
    if n < 2 {
        // The base case.
        todo!("Implement this")
    } else {
        // The recursive case.
        todo!("Implement this")
    }
}

fn main() {
    let n = 20;
    println!("fib({n}) = {}", fib(n));
}

Additional exercices Rustlings

We will be using rustlings for the additional exercises in this course.

Before starting with the exercises, you have to follow the following steps:

If you need any help, you can always ask me 😃

You should do the following Rustlings:

  • intro1
  • intro2
  • variables1
  • variables2
  • variables3
  • variables4
  • variables5

rustlings watch will guide you through these exercises in the same order. But you should stop after the last exercise listed above. The rest of the exercises will be done later.