📖 Scenario: Imagine you have a stack of books and you want to count how many books are in the stack. You can count one book at a time until there are no books left.
🎯 Goal: Build a recursive function in Rust that counts the number of books in a stack represented by a list of book titles.
📋 What You'll Learn
Create a vector called books with exactly these titles: "Book A", "Book B", "Book C"
Create a variable called count to hold the number of books counted
Write a recursive function called count_books that takes a slice of books and returns the count as usize
The function must have a base case when the slice is empty returning 0
The recursive case must count 1 plus the result of calling itself with the rest of the slice
Print the final count using println!
💡 Why This Matters
🌍 Real World
Recursion helps solve problems that can be broken into smaller similar problems, like counting items, searching, or navigating structures.
💼 Career
Understanding recursion is important for software development roles that involve algorithms, data processing, and problem solving.
Progress0 / 4 steps
1
Create the book stack
Create a vector called books with these exact titles: "Book A", "Book B", "Book C"
DSA Rust
Hint
Use vec![...] to create a vector with the given book titles.
2
Create the count variable
Create a variable called count of type usize and set it to 0 inside the main function after the books vector
DSA Rust
Hint
Use let count: usize = 0; to create the count variable.
3
Write the recursive function
Write a recursive function called count_books that takes a slice of &[&str] called books and returns a usize. The function must return 0 if the slice is empty (base case). Otherwise, return 1 plus the result of calling count_books with the rest of the slice (recursive case). Place this function above main.
DSA Rust
Hint
Use if books.is_empty() for the base case and call count_books(&books[1..]) for the recursive case.
4
Call the function and print the result
Inside main, call count_books with a slice of books and assign the result to count. Then print count using println!("{}", count).
DSA Rust
Hint
Assign count = count_books(&books); and then print it with println!("{}", count);.
Practice
(1/5)
1. In a recursive function, what is the purpose of the base case?
easy
A. To increase the input size for the next call
B. To call the function again with a smaller input
C. To print the result of the recursion
D. To stop the recursion and prevent infinite calls
Solution
Step 1: Understand the base case role
The base case is the condition that stops the recursion from continuing indefinitely.
Step 2: Differentiate from recursive case
The recursive case calls the function again with smaller input, but the base case stops recursion.
Final Answer:
To stop the recursion and prevent infinite calls -> Option D
Quick Check:
Base case = stop recursion [OK]
Hint: Base case stops recursion to avoid infinite loops [OK]
Common Mistakes:
Confusing base case with recursive case
Thinking base case increases input size
Assuming base case prints output
2. Which of the following is the correct syntax for a recursive function in Rust that calculates factorial?
B. fn factorial(n: u32) -> u32 { while n > 0 { n * factorial(n - 1) } }
C. fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } }
D. fn factorial(n: u32) -> u32 { return n + factorial(n - 1); }
Solution
Step 1: Check base case and recursive call
fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } correctly uses if n == 0 { 1 } as base case and recursive multiplication.
Step 2: Identify syntax errors in other options
fn factorial(n: u32) -> u32 { while n > 0 { n * factorial(n - 1) } } uses while loop incorrectly, C loops infinitely, D uses addition instead of multiplication.
Final Answer:
fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } -> Option C
Quick Check:
Correct base case and recursive call = fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } [OK]
Hint: Look for base case with if and recursive call with smaller input [OK]
Common Mistakes:
Using loops instead of recursion
Missing base case
Wrong operation in recursive call
3. What is the output of this Rust code?
fn sum(n: u32) -> u32 {
if n == 0 {
0
} else {
n + sum(n - 1)
}
}
fn main() {
println!("{}", sum(3));
}
The function lacks a base case like if n == 0 { 1 } to stop recursion.
Step 2: Understand recursion consequences
Without base case, recursion never stops, causing stack overflow error.
Final Answer:
Missing base case to stop recursion -> Option A
Quick Check:
Base case missing = Missing base case to stop recursion [OK]
Hint: Always include base case to stop recursion [OK]
Common Mistakes:
Assuming recursion stops automatically
Ignoring base case importance
Confusing return types
5. You want to write a recursive function in Rust to find the nth Fibonacci number. Which of the following correctly shows the base case and recursive case?
hard
A. if n == 0 { 0 } else if n == 1 { 1 } else { fib(n - 1) + fib(n - 2) }
B. if n == 1 { 0 } else if n == 2 { 1 } else { fib(n - 1) * fib(n - 2) }
C. if n == 0 { 1 } else { fib(n - 1) + fib(n - 2) }
D. if n == 0 { 0 } else { fib(n - 1) - fib(n - 2) }
Solution
Step 1: Identify correct base cases for Fibonacci
Fibonacci sequence starts with fib(0) = 0 and fib(1) = 1, so base cases must reflect that.
Step 2: Check recursive case correctness
Recursive case sums previous two Fibonacci numbers: fib(n-1) + fib(n-2).
Final Answer:
if n == 0 { 0 } else if n == 1 { 1 } else { fib(n - 1) + fib(n - 2) } -> Option A
Quick Check:
Correct base and recursive case for Fibonacci = if n == 0 { 0 } else if n == 1 { 1 } else { fib(n - 1) + fib(n - 2) } [OK]
Hint: Fibonacci base cases: 0 and 1; recursive case sums previous two [OK]
Common Mistakes:
Wrong base case values
Using multiplication or subtraction instead of addition
Missing one base case
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.