📖 Scenario: You are working on a simple program that uses recursion to process arrays and strings. Recursion means a function calls itself to solve smaller parts of a problem until it reaches a simple case.Imagine you have a list of numbers and a word. You want to find the sum of the numbers using recursion and also reverse the word using recursion.
🎯 Goal: Build a Rust program that uses recursion to sum all numbers in an array and reverse a string.
📋 What You'll Learn
Create an array of integers called numbers with the values [1, 2, 3, 4, 5].
Create a string called word with the value "hello".
Write a recursive function sum_array that takes a slice of integers and returns the sum of all elements.
Write a recursive function reverse_string that takes a string slice and returns a new String with characters in reverse order.
Print the sum of the array and the reversed string.
💡 Why This Matters
🌍 Real World
Recursion is used in many real-world problems like searching, sorting, and processing nested data structures such as file systems or JSON data.
💼 Career
Understanding recursion helps in writing clean and efficient code for algorithms and data processing tasks common in software development roles.
Progress0 / 4 steps
1
Create the data structures
Create an array called numbers with the values [1, 2, 3, 4, 5] and a string called word with the value "hello".
DSA Rust
Hint
Use let to create variables. Arrays use square brackets []. Strings can be string slices with double quotes.
2
Write the recursive sum function
Write a recursive function called sum_array that takes a slice of integers arr: &[i32] and returns an i32. It should return 0 if the slice is empty. Otherwise, return the first element plus the sum of the rest of the slice.
DSA Rust
Hint
Check if the slice is empty with arr.is_empty(). Use arr[0] for the first element and &arr[1..] for the rest.
3
Write the recursive string reverse function
Write a recursive function called reverse_string that takes a string slice s: &str and returns a String. If the string is empty, return an empty String. Otherwise, return the last character plus the reverse of the rest of the string (all except the last character).
DSA Rust
Hint
Use s.chars().last().unwrap() to get the last character. Use s.len() and last_char.len_utf8() to get the rest of the string slice.
4
Print the results
In the main function, print the sum of numbers by calling sum_array(&numbers) and print the reversed word by calling reverse_string(word). Use println! to display the results.
DSA Rust
Hint
Use println!("Sum of numbers: {}", sum_array(&numbers)) and println!("Reversed word: {}", reverse_string(word)).
Practice
(1/5)
1. What does recursion on arrays typically involve when processing elements?
easy
A. Using loops to iterate over the array elements
B. Calling the same function with a smaller part of the array until a base case is reached
C. Sorting the array before processing
D. Creating a new array for each element
Solution
Step 1: Understand recursion on arrays
Recursion on arrays means the function calls itself with a smaller portion of the array each time.
Step 2: Identify the base case
The recursion stops when the array is empty or has one element (base case).
Final Answer:
Calling the same function with a smaller part of the array until a base case is reached -> Option B
Quick Check:
Recursion = smaller input calls [OK]
Hint: Recursion breaks problem into smaller parts until simple case [OK]
Common Mistakes:
Confusing recursion with loops
Not defining a base case
Trying to process the whole array at once
2. Which of the following is the correct Rust function signature for a recursive function that takes a slice of integers?
easy
A. fn recurse(arr: &mut [i32]) -> i32
B. fn recurse(arr: Vec<i32>) -> i32
C. fn recurse(arr: i32) -> &[i32]
D. fn recurse(arr: &[i32]) -> i32
Solution
Step 1: Understand Rust slice parameter
A recursive function processing an array usually takes a slice reference like &[i32] to avoid ownership issues.
Step 2: Check return type
Returning i32 is common if the function calculates a value like sum or count.