Rust Closures and Higher-Order Functions Exercises with Solutions and Explanations
Rust Closures and Higher-Order Functions Exercises [15 exercises with solution and Explanation]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Write a Rust function that takes a closure as an argument and applies it to a given number.
2. Write a Rust function that takes a closure and two numbers, and returns the result of applying the closure to the two numbers.
3. Write a Rust function that iterates over a vector of integers and applies a closure to each element, modifying the original vector.
4. Write a Rust higher-order function that takes a closure and a vector of integers, filters the integers based on the closure predicate, and returns a new vector.
5. Write a Rust function that takes a closure and a starting value, applies the closure iteratively, and returns the final result.
6. Write a Rust function that takes a closure and a vector of strings, applies the closure to each string, and returns a new vector of modified strings.
7. Write a Rust program that creates a higher-order function that takes a closure and a range of numbers, and applies the closure to each number in the range, returning a new vector of results.
8. Write a Rust function that takes a closure and a slice of integers, applies the closure to each element, and returns the sum of the results.
9. Write a Rust function that takes a closure and two strings, concatenates the strings, and applies the closure to the concatenated string.
10. Write a Rust higher-order function that takes a closure and a vector of booleans, applies the closure to each boolean, and returns the logical AND of all results.
11. Write a Rust function that takes a closure and a vector of floats, applies the closure to each float, and returns the maximum result.
12. Write a Rust function that takes a closure and a vector of characters. Apply the closure to each character, and returns a new vector of modified characters.
13. Write a higher-order Rust function that takes a closure and a vector of tuples, applies the closure to each tuple element-wise, and returns a new vector of tuples.
14. Write a Rust function that takes a closure and a vector of values, applies the closure to each Some value, and returns a new vector of modified Option values.
15. Write a Rust function that takes a closure and a vector of arrays, applies the closure to each array element-wise, and returns a new vector of arrays.
Rust Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/rust/functional-programming/closures-and-higher-order-functions.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics