Rust-এ Common Collections: Vectors, HashMaps & Strings.
A self-motivated and enthusiastic web developer with a deep interest in JavaScript (React.js). To work in the Software industry with modern web technologies of different local & multinational Software/ IT agencies of Bangladesh and grow rapidly with increasing responsibilities.
Rust-এ data organize করার জন্য Collections খুব গুরুত্বপূর্ণ। Collections হলো এমন ধরনের data structure যা multiple value ধরে রাখতে পারে। Rust-এর কিছু common collections হল:
Vectors (
Vec<T>) – Ordered growable listHashMaps (
HashMap<K, V>) – Key-value pair storageStrings (
String) – Growable UTF-8 string
১️ Vectors (Vec<T>)
Vector হলো একটি growable array। আমরা dynamic ভাবে value add/remove করতে পারি।
fn main() {
let mut numbers: Vec<i32> = Vec::new();
numbers.push(10);
numbers.push(20);
numbers.push(30);
println!("Numbers: {:?}", numbers);
numbers.pop(); // Removes last element
println!("After pop: {:?}", numbers);
}
Vec::new()→ empty vector তৈরি করেpush→ value add করতেpop→ last value remove করতেRust compile-time-এ type ঠিক রাখে, তাই safety থাকে
কেন ব্যবহার করি:
যখন size unknown বা growable হয়
Ordered data রাখতে
২️. HashMaps (HashMap<K, V>)
HashMap হলো key-value storage। Key থেকে value lookup খুব fast।
use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
scores.insert("Alice", 50);
scores.insert("Bob", 40);
// Access
if let Some(score) = scores.get("Alice") {
println!("Alice's score: {}", score);
}
// Iterate
for (name, score) in &scores {
println!("{}: {}", name, score);
}
}
insert→ key-value add করেget→ Option type return করেIteration → &scores ব্যবহার করে borrow করা হয়
কেন ব্যবহার করি:
Lookup fast করতে key-based
Database বা dictionary style data handle করতে
৩️.Strings (String)
Rust-এ string দুই ধরনের থাকে:
&str→ immutable, stack-based string sliceString→ growable heap-based string
fn main() {
let mut s = String::from("Hello");
s.push_str(", World!"); // Append
s.push('!'); // Append single char
println!("{}", s);
// Iterate chars
for c in s.chars() {
print!("{} ", c);
}
}
String::from→ heap-allocated stringpush_str→ append stringpush→ append single charRust UTF-8 safe, so memory-safe iteration possible
কেন ব্যবহার করি:
Growable text storage
User input, messages, file content handle করতে
🔹 Summary
| Collection | Type | 특징 | Use Case |
| Vec<T> | Ordered list | Growable, index access | Dynamic array, lists |
| HashMap<K,V> | Key-value | Fast lookup, unordered | Dictionary, database-like |
| String | Text | Growable, UTF-8 | User input, messages, text storage |




