w3resource

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.

Click me to see the solution

2. Write a Rust function that takes a closure and two numbers, and returns the result of applying the closure to the two numbers.

Click me to see the solution

3. Write a Rust function that iterates over a vector of integers and applies a closure to each element, modifying the original vector.

Click me to see the solution

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.

Click me to see the solution

5. Write a Rust function that takes a closure and a starting value, applies the closure iteratively, and returns the final result.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

9. Write a Rust function that takes a closure and two strings, concatenates the strings, and applies the closure to the concatenated string.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.



Follow us on Facebook and Twitter for latest update.