📖 Scenario: You are helping a friend understand how to calculate Fibonacci numbers using a simple function that calls itself.The Fibonacci sequence starts with 0 and 1, and each next number is the sum of the two before it.
🎯 Goal: Build a Rust program that uses a recursive function to find the Fibonacci number at a given position.
📋 What You'll Learn
Create a function called fibonacci that takes an usize parameter n and returns a usize.
Use recursion inside the fibonacci function to calculate the Fibonacci number.
Call the fibonacci function with the value 7 and store the result in a variable called result.
Print the value of result.
💡 Why This Matters
🌍 Real World
Fibonacci numbers appear in nature, computer algorithms, and financial models. Understanding recursion helps solve problems that break down into smaller similar problems.
💼 Career
Many programming jobs require understanding recursion and how to implement it efficiently, especially in algorithm design and problem solving.
Progress0 / 4 steps
1
Create the Fibonacci function signature
Write a function called fibonacci that takes a parameter n of type usize and returns a usize. For now, make it return 0.
DSA Rust
Hint
Start by writing the function header and return 0 inside the function body.
2
Add base cases for recursion
Inside the fibonacci function, add two base cases: if n is 0, return 0; if n is 1, return 1.
DSA Rust
Hint
Use if and else if to check for 0 and 1 and return the correct values.
3
Add recursive call to calculate Fibonacci
In the fibonacci function, after the base cases, return the sum of fibonacci(n - 1) and fibonacci(n - 2).
DSA Rust
Hint
Return the sum of the function called with n - 1 and n - 2.
4
Call the function and print the result
Call the fibonacci function with the value 7 and store the result in a variable called result. Then print result.
DSA Rust
Hint
Use let result = fibonacci(7); and println!("{}", result); inside a main function.
Practice
(1/5)
1. What does the Fibonacci sequence represent when calculated using recursion?
easy
A. Each number is the square of the previous number
B. Each number is the product of the two preceding numbers
C. Each number is the difference of the two preceding numbers
D. Each number is the sum of the two preceding numbers
Solution
Step 1: Understand Fibonacci sequence definition
The Fibonacci sequence is defined such that each number is the sum of the two numbers before it.
Step 2: Relate to recursion
Recursion calls the function for the two previous numbers and adds their results.
Final Answer:
Each number is the sum of the two preceding numbers -> Option D
Quick Check:
Fibonacci = sum of two previous numbers [OK]
Hint: Fibonacci adds last two numbers, not multiply or subtract [OK]
Common Mistakes:
Thinking Fibonacci multiplies previous numbers
Confusing subtraction or division with addition
Assuming Fibonacci squares previous number
2. Which of the following is the correct Rust function signature for a recursive Fibonacci function?
easy
A. fn fibonacci(n: i32) -> i32
B. fn fibonacci(n) -> i32
C. fn fibonacci(n: i32)
D. fn fibonacci(n: i32) -> void
Solution
Step 1: Check Rust function signature syntax
Rust requires parameter types and return type specified, e.g., fn name(param: type) -> return_type.
Step 2: Match correct signature for Fibonacci
Fibonacci takes an integer and returns an integer, so fn fibonacci(n: i32) -> i32 is correct.
Final Answer:
fn fibonacci(n: i32) -> i32 -> Option A
Quick Check:
Rust needs types and return type [OK]
Hint: Rust functions need parameter and return types declared [OK]
Common Mistakes:
Omitting parameter type
Missing return type
Using void instead of () or proper return type
3. What is the output of this Rust code?
fn fibonacci(n: i32) -> i32 {
if n <= 1 {
n
} else {
fibonacci(n - 1) + fibonacci(n - 2)
}
}
fn main() {
println!("{}", fibonacci(5));
}