📖 Scenario: Imagine you are stacking boxes one on top of another. To find out how many boxes are stacked, you can count them one by one from the top to the bottom. Recursion works similarly by breaking a problem into smaller parts and solving each part step-by-step.
🎯 Goal: You will build a simple recursive function in Rust that counts down from a given number to 1, printing each step. This will help you see how recursion calls itself and how the call stack grows and shrinks.
📋 What You'll Learn
Create a recursive function called count_down that takes an integer parameter n.
Use a base case to stop recursion when n is 0.
Print the current value of n in each recursive call.
Call count_down starting from 5.
Print the final message "Done!" after recursion finishes.
💡 Why This Matters
🌍 Real World
Recursion is used in many real-world problems like searching files in folders, solving puzzles, and processing tree structures.
💼 Career
Understanding recursion helps in software development roles that involve algorithms, data structures, and problem-solving.
Progress0 / 4 steps
1
Create the starting point variable
Create a variable called start and set it to 5.
DSA Rust
Hint
Use let start = 5; inside the main function.
2
Define the recursive function
Define a function called count_down that takes an integer parameter n. Inside it, write an if statement that stops recursion when n == 0. Otherwise, print n and call count_down(n - 1).
DSA Rust
Hint
Use fn count_down(n: i32) and inside it check if n == 0 to stop. Print n and call count_down(n - 1) recursively.
3
Call the recursive function
Inside the main function, call count_down with the variable start.
DSA Rust
Hint
Call the function by writing count_down(start); inside main.
4
Print completion message
After calling count_down(start) in main, print "Done!" to show recursion finished.
DSA Rust
Hint
Use println!("Done!"); after the recursive call in main.
Practice
(1/5)
1. What happens when a recursive function calls itself without a base case?
easy
A. The function converts into a loop automatically.
B. The function returns immediately without doing anything.
C. The function runs only once and stops.
D. It causes infinite recursion leading to a stack overflow error.
Solution
Step 1: Understand recursion base case role
The base case stops recursion by providing a condition to end calls.
Step 2: Analyze missing base case effect
Without a base case, the function keeps calling itself endlessly, filling the call stack.
Final Answer:
It causes infinite recursion leading to a stack overflow error. -> Option D
Quick Check:
Missing base case = infinite recursion [OK]
Hint: Always check for a base case to stop recursion [OK]
Common Mistakes:
Thinking recursion stops automatically
Assuming function returns without base case
Confusing recursion with loops
2. Which of the following is the correct syntax to define a recursive function in Rust that calculates factorial of a number n?
easy
A. fn factorial(n: u32) { if n == 0 { 1 } else { n * factorial(n - 1) } }
B. fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } }
C. fn factorial(n: u32) -> u32 { while n > 0 { n * factorial(n - 1) } }
D. fn factorial(n: u32) -> u32 { return n * factorial(n + 1); }
Solution
Step 1: Check function return type and base case
fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } correctly returns u32 and has base case for n == 0 returning 1.
Step 2: Verify recursive call correctness
fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } calls factorial(n - 1) correctly; others miss return type or have wrong logic.
Final Answer:
fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } -> Option B
Quick Check:
Correct syntax = fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } [OK]
Hint: Look for base case and correct return type in Rust function [OK]
Common Mistakes:
Omitting return type in function signature
Using while loop inside recursive function
Calling factorial with n + 1 causing infinite recursion