Programming Languages
Rust
Subjective
Oct 04, 2025
How do you handle collections and their common operations in Rust?
Detailed Explanation
Common collections in Rust:
• Vec: growable arrays
• HashMap: key-value pairs
• HashSet: unique values
• BTreeMap/BTreeSet: ordered collections
• VecDeque: double-ended queue
Example:
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Red"), 50);
// Access
let score = scores.get("Blue");
// Iterate
for (key, value) in &scores {
println!("{}: {}", key, value);
}
// Update
scores.entry(String::from("Yellow")).or_insert(50);
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts