Skip to main content

Command Palette

Search for a command to run...

Rust-এ Common Collections: Vectors, HashMaps & Strings.

Published
2 min read
M

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 হল:

  1. Vectors (Vec<T>) – Ordered growable list

  2. HashMaps (HashMap<K, V>) – Key-value pair storage

  3. Strings (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 দুই ধরনের থাকে:

  1. &str → immutable, stack-based string slice

  2. String → 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 string

  • push_str → append string

  • push → append single char

  • Rust UTF-8 safe, so memory-safe iteration possible

কেন ব্যবহার করি:

  • Growable text storage

  • User input, messages, file content handle করতে


🔹 Summary

CollectionType특징Use Case
Vec<T>Ordered listGrowable, index accessDynamic array, lists
HashMap<K,V>Key-valueFast lookup, unorderedDictionary, database-like
StringTextGrowable, UTF-8User input, messages, text storage

More from this blog

DSA মানে শুধু LeetCode না — DSA মানে Production System slow না করা

অনেকেই বলে — “ভাই, Web Development-এ DSA লাগে না” আসলে Junior Developer থাকলে এমনটাই মনে হয়।আমি নিজেও Junior থাকতে তাই ভাবতাম। আমার আবার একটা বাজে স্বভাব আছে 😅👉 কোন কিছুর বাস্তব প্রয়োজন না বুঝলে সেটা আমার মাথায় ঢুকে না। Junior থাকতে DSA শিখেছি...

Dec 24, 20253 min read
DSA মানে শুধু LeetCode না — DSA মানে Production System slow না করা

🦀 Rust-এ মেমরি ম্যানেজমেন্ট: GC ছাড়া In-depth.

Rust-এ Garbage Collector (GC) নেই, কিন্তু মেমরি নিরাপত্তা (memory safety) আর performance Rust-এর সবচেয়ে শক্তিশালী দিক।তাই, Go-এর মতো runtime-based GC না থাকা সত্ত্বেও Rust compile-time এ মেমরি ম্যানেজ করে Ownership System, Borrow Checker, আর Lifetime...

Oct 5, 202523 min read
🦀 Rust-এ মেমরি ম্যানেজমেন্ট: GC ছাড়া In-depth.

Morshedul Munna

45 posts

As a Software Developer, I am like an architect who designs and builds digital Products. I use my knowledge and expertise to create modern applications that are both efficient and elegant.