Rust Iterators Quiz
Master Rust's powerful iterator system for efficient data processing and transformation
Question 1
What is the Iterator trait in Rust?
Question 2
What is the signature of the next() method in Iterator?
Question 3
What does this iterator code produce?
let v = vec![1, 2, 3];
let mut iter = v.iter();
println!("{:?}", iter.next());
println!("{:?}", iter.next());Question 4
What is the difference between iter() and into_iter()?
Question 5
What are iterator adapters?
Question 6
What does the map() adapter do?
Question 7
What does this map() example produce?
let v = vec![1, 2, 3];
let doubled: Vec<i32> = v.iter().map(|x| x * 2).collect();Question 8
What does the filter() adapter do?
Question 9
What does this filter() example produce?
let v = vec![1, 2, 3, 4, 5];
let even: Vec<i32> = v.into_iter().filter(|x| x % 2 == 0).collect();Question 10
What are iterator consumers?
Question 11
What does the collect() consumer do?
Question 12
What does this collect() example do?
let v = vec![1, 2, 3];
let doubled: Vec<i32> = v.iter().map(|&x| x * 2).collect();Question 13
What does the sum() consumer do?
Question 14
What does this sum() example produce?
let v = vec![1, 2, 3, 4];
let total: i32 = v.iter().sum();Question 15
What does the count() consumer do?
Question 16
What is lazy evaluation in iterators?
Question 17
What happens in this lazy evaluation example?
let v = vec![1, 2, 3, 4, 5];
let result: Vec<i32> = v.iter()
.map(|x| {
println!("Mapping {}", x);
x * 2
})
.filter(|x| {
println!("Filtering {}", x);
x > &4
})
.collect();Question 18
How do you create a custom iterator?
Question 19
What is required to implement a custom iterator?
Question 20
What does this custom iterator example do?
struct Counter {
count: u32,
}
impl Iterator for Counter {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
self.count += 1;
if self.count < 6 {
Some(self.count)
} else {
None
}
}
}Question 21
What is the IntoIterator trait?
Question 22
What is the difference between Iterator and IntoIterator?
Question 23
What does the enumerate() adapter do?
Question 24
What does this enumerate() example produce?
let v = vec!['a', 'b', 'c'];
let enumerated: Vec<(usize, char)> = v.iter().enumerate().collect();Question 25
What does the zip() adapter do?
Question 26
What does this zip() example produce?
let a = vec![1, 2, 3];
let b = vec!['a', 'b'];
let zipped: Vec<(i32, char)> = a.iter().zip(b.iter()).collect();Question 27
What does the take() adapter do?
Question 28
What does the skip() adapter do?
Question 29
What does the chain() adapter do?
Question 30
What does this chain() example produce?
let a = vec![1, 2];
let b = vec![3, 4];
let chained: Vec<i32> = a.iter().chain(b.iter()).collect();Question 31
What does the fold() consumer do?
Question 32
What does this fold() example produce?
let v = vec![1, 2, 3, 4];
let sum = v.iter().fold(0, |acc, x| acc + x);Question 33
What does the any() consumer do?
Question 34
What does the all() consumer do?
Question 35
What does the find() consumer do?
Question 36
What is iterator performance like compared to loops?
Question 37
What is the advantage of lazy evaluation?
Question 38
How do you implement IntoIterator for a custom type?
Question 39
What is the difference between iter() and iter_mut()?
Question 40
In a data processing pipeline where you need to filter a large dataset, transform each element, and collect only the first 10 results, which iterator approach would be most efficient and why?
