w3resource

Rust Iterator Exercises with Solutions and Explanations

Rust Iterators and Iterator Adapters 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 program that iterates over a vector of integers and prints each element.

Click me to see the solution

2. Write a Rust program that iterates over a range of numbers and calculates the sum of all numbers.

Click me to see the solution

3. Write a Rust program that iterates over a vector of strings and prints the length of each string.

Click me to see the solution

4. Write a Rust program that iterates over a vector of tuples (i32, i32) and calculates the product of the first and second elements of each tuple.

Click me to see the solution

5. Write a Rust program that iterates over a vector of booleans and counts the number of false and true values.

Click me to see the solution

6. Write a Rust program that iterates over a vector of floats and calculates the average value.

Click me to see the solution

7. Write a Rust program that iterates over a vector of Option values and prints the value of each Some variant.

Click me to see the solution

8. Write a Rust program that iterates over a vector of arrays [i32; 2] and calculates the sum of the first and second elements of each array.

Click me to see the solution

9. Write a Rust program that iterates over a range of numbers and filters out odd numbers.

Click me to see the solution

10. Write a Rust program that iterates over a vector of strings and converts each string to uppercase.

Click me to see the solution

11. Write a Rust program that iterates over a vector of integers and cubes for each element.

Click me to see the solution

12. Write a Rust program that iterates over a vector of tuples (i32, i32) and swaps the elements of each tuple.

Click me to see the solution

13. Write a Rust program that iterates over a vector of Option values and returns a new vector containing only the Some values.

Click me to see the solution

14. Write a Rust program that iterates over a vector of arrays [i32; 2] and calculates the difference between the first and second elements of each array.

Click me to see the solution

15. Write a Rust program that iterates over a range of numbers and checks if each number is prime. Return a new vector containing only prime numbers.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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/iteretors-and-iterator-adapters.php