Rust Collections Quiz
40 comprehensive questions on Rust's collection types, covering Vec, HashMap, VecDeque, iteration patterns, and capacity management — with 11 code examples demonstrating practical collection usage.
Question 1
What is a Vec in Rust?
Question 2
How do you create an empty Vec?
Question 3
How do you create a Vec with initial values?
Question 4
What will this code do?
fn main() {
let v = vec![1, 2, 3];
println!("First element: {}", v[0]);
}Question 5
What happens when you access an invalid index in a Vec?
Question 6
How do you safely access Vec elements?
Question 7
How do you add elements to a Vec?
Question 8
What does pop() do on a Vec?
Question 9
How do you iterate over a Vec?
Question 10
What is the difference between iter() and iter_mut()?
Question 11
What will this code do?
fn main() {
let mut v = vec![1, 2, 3];
for x in &mut v {
*x += 1;
}
println!("{:?}", v);
}Question 12
What is a HashMap in Rust?
Question 13
How do you create an empty HashMap?
Question 14
How do you insert into a HashMap?
Question 15
How do you get a value from a HashMap?
Question 16
What happens when you access a non-existent key with []?
Question 17
How do you check if a key exists in a HashMap?
Question 18
What does entry() method do?
Question 19
What will this code do?
use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
scores.insert("Alice", 10);
scores.insert("Alice", 20);
println!("Alice's score: {}", scores[&"Alice"]);
}Question 20
What is a VecDeque?
Question 21
How do you add to the front of a VecDeque?
Question 22
What does pop_back() do on VecDeque?
Question 23
When should you use VecDeque instead of Vec?
Question 24
What is capacity in collections?
Question 25
How do you reserve capacity in a Vec?
Question 26
What happens when you exceed Vec capacity?
Question 27
How do you shrink a Vec to fit its contents?
Question 28
What is the difference between len() and capacity()?
Question 29
How do you iterate over a HashMap?
Question 30
What does into_iter() do?
Question 31
How do you sort a Vec?
Question 32
What does retain() do on a Vec?
Question 33
How do you remove elements from a Vec by index?
Question 34
What is the entry API pattern?
Question 35
How do you clear all elements from a collection?
Question 36
What is the most efficient way to build a large Vec?
Question 37
How do you check if a Vec is empty?
Question 38
What does dedup() do on a Vec?
Question 39
How do you convert between collections?
Question 40
When should you use HashMap over Vec?
