
Practice 50 Closures and Higher-Order Functions Coding Questions
Q1. Write a Rust program to define a closure that takes two integers and returns their sum.
Input:
Integers: 3, 4
Expected Output:
Sum: 7
Q2. Write a Rust program to define a closure that checks if a number is even or odd.
Input:
Number: 7
Expected Output:
Odd
Q3. Write a Rust program to use a closure that multiplies a number by 2.
Input:
Number: 5
Expected Output:
Result: 10
Q4. Write a Rust program that passes a closure to a function to calculate the square of a number.
Input:
Number: 6
Expected Output:
Square: 36
Q5. Write a Rust program to create a closure that concatenates two strings.
Input:
Strings: "Hello", "World"
Expected Output:
Concatenated string: "HelloWorld"
Q6. Write a Rust program to define a closure that filters out numbers greater than a threshold from a list of numbers.
Input:
List: [1, 5, 3, 8, 6], Threshold: 4
Expected Output:
Filtered list: [5, 8, 6]
Q7. Write a Rust program to pass a closure to a function that calculates the factorial of a number.
Input:
Number: 4
Expected Output:
Factorial: 24
Q8. Write a Rust program to define a closure that reverses a string.
Input:
String: "Rust"
Expected Output:
Reversed string: "tsuR"
Q9. Write a Rust program to use a closure that computes the nth Fibonacci number.
Input:
Number: 6
Expected Output:
Fibonacci number: 8
Q10. Write a Rust program to define a closure that finds the maximum of two numbers.
Input:
Numbers: 10, 20
Expected Output:
Maximum: 20
Q11. Write a Rust program to pass a closure to a function that calculates the sum of a list of numbers.
Input:
List: [1, 2, 3, 4]
Expected Output:
Sum: 10
Q12. Write a Rust program that demonstrates the use of a closure with a mutable environment to update a variable.
Input:
Initial variable: 5
Expected Output:
Updated value: 10
Q13. Write a Rust program that defines a closure which checks whether a string is a palindrome.
Input:
String: "madam"
Expected Output:
Palindrome: true
Q14. Write a Rust program to use a closure that divides two numbers and handles division by zero.
Input:
Numbers: 10, 2
Expected Output:
Division result: 5
Q15. Write a Rust program that defines a higher-order function that takes a closure and applies it to a number.
Input:
Number: 8, Closure: Multiply by 3
Expected Output:
Result: 24
Q16. Write a Rust program to define a closure that checks if a number is a prime number.
Input:
Number: 11
Expected Output:
Prime: true
Q17. Write a Rust program that defines a closure to calculate the average of a list of numbers.
Input:
List: [2, 4, 6, 8, 10]
Expected Output:
Average: 6
Q18. Write a Rust program that uses a closure to subtract two numbers.
Input:
Numbers: 10, 3
Expected Output:
Difference: 7
Q19. Write a Rust program to define a closure that squares a number and adds 5 to it.
Input:
Number: 4
Expected Output:
Result: 21
Q20. Write a Rust program that defines a closure which checks if a number is positive.
Input:
Number: -5
Expected Output:
Positive: false
Q21. Write a Rust program to create a closure that accepts two numbers and returns their product.
Input:
Numbers: 3, 4
Expected Output:
Product: 12
Q22. Write a Rust program that defines a closure which capitalizes the first letter of a string.
Input:
String: "rust"
Expected Output:
Capitalized string: "Rust"
Q23. Write a Rust program that uses a closure to filter out all odd numbers from a list.
Input:
List: [1, 2, 3, 4, 5]
Expected Output:
Even numbers: [2, 4]
Q24. Write a Rust program that defines a closure which checks if a number is divisible by another number.
Input:
Numbers: 15, 5
Expected Output:
Divisible: true
Q25. Write a Rust program that passes a closure to a function and applies it to an array of numbers.
Input:
Array: [1, 2, 3], Closure: Add 2 to each number
Expected Output:
Modified array: [3, 4, 5]
Q26. Write a Rust program that defines a higher-order function which accepts a closure to compare two strings.
Input:
Strings: "apple", "banana"
Expected Output:
Comparison result: "apple" is smaller
Q27. Write a Rust program to define a closure that multiplies each element of a vector by 2.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
Modified vector: [2, 4, 6, 8]
Q28. Write a Rust program that defines a closure to filter out even numbers from a vector.
Input:
Vector: [1, 2, 3, 4, 5, 6]
Expected Output:
Filtered vector: [1, 3, 5]
Q29. Write a Rust program that defines a closure which returns the length of a string.
Input:
String: "hello"
Expected Output:
Length: 5
Q30. Write a Rust program that defines a higher-order function that applies a closure to filter a list of strings.
Input:
List: ["apple", "banana", "pear"], Closure: Filter strings with length > 4
Expected Output:
Filtered list: ["banana"]
Q31. Write a Rust program to define a closure to find the minimum of three numbers.
Input:
Numbers: 4, 2, 7
Expected Output:
Minimum: 2
Q32. Write a Rust program that defines a closure that checks if a string starts with a particular character.
Input:
String: "Rust", Character: "R"
Expected Output:
Starts with R: true
Q33. Write a Rust program that uses a closure to find the maximum of three numbers.
Input:
Numbers: 4, 8, 2
Expected Output:
Maximum: 8
Q34. Write a Rust program that defines a closure that returns a number raised to a given power.
Input:
Number: 2, Power: 3
Expected Output:
Result: 8
Q35. Write a Rust program to define a closure that reverses an array.
Input:
Array: [1, 2, 3]
Expected Output:
Reversed array: [3, 2, 1]
Q36. Write a Rust program that defines a higher-order function to find the product of elements in a list using closures.
Input:
List: [1, 2, 3, 4]
Expected Output:
Product: 24
Q37. Write a Rust program to create a closure that concatenates a list of strings into one string.
Input:
Strings: ["Hello", "World"]
Expected Output:
Concatenated string: "HelloWorld"
Q38. Write a Rust program that defines a closure to check if a string is in uppercase.
Input:
String: "HELLO"
Expected Output:
Uppercase: true
Q39. Write a Rust program that uses a closure to double every number in a vector.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
Doubled vector: [2, 4, 6, 8]
Q40. Write a Rust program to define a closure that returns the nth power of a given number.
Input:
Number: 3, Power: 4
Expected Output:
Result: 81
Q41. Write a Rust program to use a closure to find the sum of all elements in a vector.
Input:
Vector: [1, 2, 3]
Expected Output:
Sum: 6
Q42. Write a Rust program to use a closure to check if an element exists in a vector.
Input:
Vector: [1, 2, 3, 4], Element: 3
Expected Output:
Element exists: true
Q43. Write a Rust program that defines a higher-order function to apply a closure to a string.
Input:
String: "Hello", Closure: Convert to uppercase
Expected Output:
Uppercase: "HELLO"
Q44. Write a Rust program to define a closure that squares a number.
Input:
Number: 5
Expected Output:
Square: 25
Q45. Write a Rust program to define a closure that checks if a number is divisible by 3.
Input:
Number: 9
Expected Output:
Divisible by 3: true
Q46. Write a Rust program that defines a closure to subtract two numbers and returns the result.
Input:
Numbers: 10, 3
Expected Output:
Difference: 7
Q47. Write a Rust program that defines a closure to concatenate a list of integers as strings.
Input:
List: [1, 2, 3]
Expected Output:
Concatenated string: "123"
Q48. Write a Rust program to use a closure to find the product of all elements in a vector.
Input:
Vector: [1, 2, 3]
Expected Output:
Product: 6
Q49. Write a Rust program to define a closure that divides two numbers and prints an error for division by zero.
Input:
Numbers: 10, 0
Expected Output:
Error: Division by zero
Q50. Write a Rust program that uses a closure to find the difference between the largest and smallest number in a vector.
Input:
Vector: [1, 5, 3, 9, 2]
Expected Output:
Difference: 8