
Practice 50 Iteretors and Iterator Adapters Coding Questions
Q1. Write a Rust program to create a simple iterator that generates the first 5 numbers in a range.
Expected Output:
1 2 3 4 5
Q2. Write a Rust program to filter even numbers from a vector using an iterator.
Input:
Vector: [1, 2, 3, 4, 5, 6]
Expected Output:
2 4 6
Q3. Write a Rust program to use the .map() method to square all the elements of a vector.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
1 4 9 16
Q4. Write a Rust program to use .filter() and .count() to find how many numbers in a vector are greater than 3.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
3
Q5. Write a Rust program to use .fold() to sum all the numbers in a vector.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
Sum: 15
Q6. Write a Rust program to use .map() to add 5 to each element in a vector.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
6 7 8 9
Q7. Write a Rust program to use .collect() to convert a vector into a HashSet.
Input:
Vector: [1, 2, 3, 3, 4]
Expected Output:
{1, 2, 3, 4}
Q8. Write a Rust program to use .skip() to skip the first 3 elements of a vector and return the remaining.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
4 5
Q9. Write a Rust program to use .take() to take the first 3 elements of a vector.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
1 2 3
Q10. Write a Rust program to use .zip() to combine two vectors into a tuple iterator.
Input:
Vector 1: [1, 2, 3], Vector 2: [4, 5, 6]
Expected Output:
(1, 4) (2, 5) (3, 6)
Q11. Write a Rust program to use .rev() to reverse a vector using an iterator.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
4 3 2 1
Q12. Write a Rust program to use .all() to check if all elements in a vector are greater than 0.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
true
Q13. Write a Rust program to use .any() to check if any element in a vector is less than 0.
Input:
Vector: [1, -2, 3, 4]
Expected Output:
true
Q14. Write a Rust program to use .find() to search for the first element that is greater than 3 in a vector.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
4
Q15. Write a Rust program to use .position() to find the position of the first element that is equal to 3 in a vector.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
2
Q16. Write a Rust program to use .take_while() to take elements from the vector while they are less than 4.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
1 2 3
Q17. Write a Rust program to use .skip_while() to skip elements while they are less than 4 and return the remaining.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
4 5
Q18. Write a Rust program to use .chain() to concatenate two iterators into one.
Input:
Iterator 1: [1, 2], Iterator 2: [3, 4]
Expected Output:
1 2 3 4
Q19. Write a Rust program to use .map() and .collect() to convert all numbers in a vector into strings.
Input:
Vector: [1, 2, 3]
Expected Output:
["1", "2", "3"]
Q20. Write a Rust program to use .flat_map() to flatten a vector of vectors into a single vector.
Input:
Vector: [[1, 2], [3, 4], [5, 6]]
Expected Output:
[1, 2, 3, 4, 5, 6]
Q21. Write a Rust program to use .take() to take the first 2 elements of a vector and print them.
Input:
Vector: [5, 10, 15, 20]
Expected Output:
5 10
Q22. Write a Rust program to use .filter_map() to filter and transform the elements of a vector.
Input:
Vector: [Some(1), None, Some(2), Some(3)]
Expected Output:
1 2 3
Q23. Write a Rust program to use .scan() to generate a cumulative sum of a vector.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
1 3 6 10
Q24. Write a Rust program to use .enumerate() to iterate over the elements of a vector along with their indices.
Input:
Vector: [10, 20, 30]
Expected Output:
(0, 10) (1, 20) (2, 30)
Q25. Write a Rust program to use .cloned() to create an iterator that clones the elements of a vector.
Input:
Vector: ["a", "b", "c"]
Expected Output:
"a" "b" "c"
Q26. Write a Rust program to use .collect() to gather the results of filtering even numbers from a vector into a new vector.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
[2, 4]
Q27. Write a Rust program to use .map() to multiply all the elements of a vector by 2.
Input:
Vector: [1, 2, 3]
Expected Output:
2 4 6
Q28. Write a Rust program to use .reduce() to calculate the product of all elements in a vector.
Input:
Vector: [2, 3, 4]
Expected Output:
24
Q29. Write a Rust program to use .partition() to split a vector into two groups, one with even numbers and the other with odd numbers.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
([2, 4], [1, 3, 5])
Q30. Write a Rust program to use .sum() to calculate the sum of a vector of floating-point numbers.
Input:
Vector: [1.1, 2.2, 3.3]
Expected Output:
Sum: 6.6
Q31. Write a Rust program to use .take_while() to take elements until a condition is met (less than 4).
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
1 2 3
Q32. Write a Rust program to use .inspect() to inspect and print each element during iteration.
Input:
Vector: [1, 2, 3]
Expected Output:
1 2 3
Q33. Write a Rust program to use .find() to search for an element in a vector.
Input:
Vector: [1, 2, 3, 4], Target: 3
Expected Output:
3
Q34. Write a Rust program to use .all() to check if all elements in a vector are greater than 0.
Input:
Vector: [1, 2, 3]
Expected Output:
true
Q35. Write a Rust program to use .take_while() to take elements until a condition is met and print the result.
Input:
Vector: [1, 2, 3, 4], Condition: < 4
Expected Output:
1 2 3
Q36. Write a Rust program to use .skip() to skip the first two elements of a vector.
Input:
Vector: [10, 20, 30, 40]
Expected Output:
30 40
Q37. Write a Rust program to use .map() to increment each element in a vector by 1.
Input:
Vector: [1, 2, 3]
Expected Output:
2 3 4
Q38. Write a Rust program to use .all() to check if all elements in a vector are divisible by 2.
Input:
Vector: [2, 4, 6]
Expected Output:
true
Q39. Write a Rust program to use .any() to check if any number in a vector is divisible by 3.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
true
Q40. Write a Rust program to use .position() to find the position of the first element greater than 5.
Input:
Vector: [1, 2, 3, 6, 7]
Expected Output:
3
Q41. Write a Rust program to use .filter() to filter out all even numbers from a vector.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
1 3 5
Q42. Write a Rust program to use .cloned() to clone a vector of strings.
Input:
Vector: ["a", "b", "c"]
Expected Output:
"a" "b" "c"
Q43. Write a Rust program to use .find() to return the first element greater than 3 in a vector.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
4
Q44. Write a Rust program to use .take_while() to take elements less than 4 from a vector.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
1 2 3
Q45. Write a Rust program to use .partition() to split a vector into two parts, based on a condition.
Input:
Vector: [1, 2, 3, 4, 5], Condition: x > 3
Expected Output:
([4, 5], [1, 2, 3])
Q46. Write a Rust program to use .scan() to generate the cumulative sum of elements in a vector.
Input:
Vector: [1, 2, 3]
Expected Output:
1 3 6
Q47. Write a Rust program to use .map() to multiply all elements by 3.
Input:
Vector: [1, 2, 3]
Expected Output:
3 6 9
Q48. Write a Rust program to use .enumerate() to print each element along with its index.
Input:
Vector: [10, 20, 30]
Expected Output:
(0, 10) (1, 20) (2, 30)
Q49. Write a Rust program to use .fold() to concatenate all elements of a vector into a string.
Input:
Vector: ["a", "b", "c"]
Expected Output:
abc
Q50. Write a Rust program to use .inspect() to print elements of a vector during iteration.
Input:
Vector: [5, 6, 7]
Expected Output:
5 6 7