Rust Collections — Vectors, Strings, HashMaps, and Collection Operations
DodaTech
Updated 2026-06-28
1 min read
In this tutorial, you will learn about Rust Collections. We cover key concepts, practical examples, and best practices to help you master this topic.
Rust collections include Vec for dynamic arrays, String for UTF-8 text, and HashMap for key-value storage with iterators and common methods.
What You'll Learn
- Vectors (Vec
) - Strings (String, &str)
- HashMaps (HashMap<K, V>)
- Iterator operations
Why It Matters
Collections are fundamental to programming. DodaZIP uses Vec for file buffers and HashMap for metadata indexing.
Real-World Use
Data storage, Configuration Management, text processing, Caching.
use std::collections::HashMap;
fn main() {
// Vectors
let mut v: Vec<i32> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
let third = &v[2];
println!("Third: {}", third);
for i in &v { println!("{}", i); }
// Strings
let mut s = String::from("hello");
s.push_str(", world");
s.push('!');
println!("{}", s);
// HashMap
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
let team_name = String::from("Blue");
match scores.get(&team_name) {
Some(score) => println!("Score: {}", score),
None => println!("Team not found"),
}
for (key, value) in &scores {
println!("{}: {}", key, value);
}
// Iterator operations
let numbers = vec![1, 2, 3, 4, 5];
let doubled: Vec<i32> = numbers.iter().map(|x| x * 2).collect();
let evens: Vec<&i32> = numbers.iter().filter(|x| *x % 2 == 0).collect();
let sum: i32 = numbers.iter().sum();
println!("{:?}, {:?}, {}", doubled, evens, sum);
}
← Previous
Rust Cargo — Package Management with Cargo.toml, Dependencies, and Build System
Next →
Rust Testing — Unit Tests, Integration Tests, Doc Tests, and Test Organization
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro