Skip to main content

Command Palette

Search for a command to run...

Rust-এ Error Handling ও Nullable Values সহজভাবে

Published
3 min read
Rust-এ Error Handling ও Nullable Values সহজভাবে
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 হল এমন একটি language যা memory safe এবং performance oriented। এখানে কোনো garbage collector নেই, কিন্তু Rust compile-time-এ এমন সব check করে যাতে dangling pointer, null pointer, double free, বা data race এর মতো সমস্যাগুলো কখনো ঘটে না।

Rust এ আমরা সাধারণত দুই ধরনের situation handle করি:

  1. Value থাকতে পারে বা নাও থাকতে পারে → Option<T>

  2. Function failure হতে পারে → Result<T, E>


১. Option<T> – Nullable Value

Option<T> হলো Rust-এর enum যা দুইটা state দেখায়:

  • Some(value) → Value আছে

  • None → Value নেই

উদাহরণ:

fn find_index(arr: &[i32], target: i32) -> Option<usize> {
    for (i, &val) in arr.iter().enumerate() {
        if val == target {
            return Some(i);
        }
    }
    None
}

ব্যাখ্যা:

  • এখানে যদি target element পাওয়া যায়, আমরা Some(index) return করি।

  • না পেলে None return করি।

  • Rust compile-time-এ জানে Option<T> কতটা memory নেবে। যদি T pointer হয়, তাহলে extra memory লাগে না।

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

  • Null value handle করতে safely

  • Null pointer crash থেকে বাঁচাতে


enumerate কি?

enumerate হলো Rust-এর iterator method, যা মূলত loop চলাকালীন index এবং value দুটোই দেয়

সাধারণত আমরা for loop দিয়ে array বা vector iterate করি:

let arr = [10, 20, 30];

for val in arr.iter() {
    println!("{}", val);
}

এখানে শুধু value পাই। কিন্তু কখনো কখনো index ও value দুটোই চাই। তখন আমরা enumerate() ব্যবহার করি:

let arr = [10, 20, 30];

for (i, val) in arr.iter().enumerate() {
    println!("Index: {}, Value: {}", i, val);
}

Output:

Index: 0, Value: 10
Index: 1, Value: 20
Index: 2, Value: 30

ব্যাখ্যা:

  • arr.iter() → array বা vector এর iterator দেয়।

  • .enumerate() → প্রতিটা element কে একটি tuple (index, value) আকারে রূপান্তর করে।

  • (i, val) → tuple destructure করে index ও value আলাদাভাবে ব্যবহার করা যায়।

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

  • Loop-এ কোন element এর position বা index জানতে চাইলে

  • Data search বা debug করার সময় handy


২. Result<T, E> – Error Handle

Result<T, E> হলো Rust-এর enum যা দেখায় function সফল হয়েছে কিনা:

  • Ok(value) → Success

  • Err(error) → Failure

উদাহরণ:

fn divide(a: i32, b: i32) -> Result<i32, String> {
    if b == 0 {
        Err("Division by zero".to_string())
    } else {
        Ok(a / b)
    }
}

ব্যাখ্যা:

  • যদি denominator 0 হয়, আমরা error return করি।

  • Rust compile-time-এ memory ঠিক করে রাখে, extra overhead নেই।

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

  • Recoverable error handle করতে

  • Panic (program crash) না করে error propagate করতে


৩. match – Pattern Matching

match Rust-এর একদম core feature। এটি enum value handle করতে ব্যবহৃত হয়।

let result = divide(10, 2);

match result {
    Ok(value) => println!("Result is {}", value),
    Err(e) => println!("Error: {}", e),
}

ব্যাখ্যা:

  • Rust compile-time-এ check করে যে সব possible case handle করা হয়েছে কি না।

  • CPU-level এ efficient branch তৈরি হয়।

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

  • Value বা Error safely handle করতে

  • Code readable এবং safe রাখতে


৪. ? Operator – সহজ Error Propagation

? হলো shorthand যেটা result কে automatically check করে, error হলে function থেকে return করে।

fn divide_and_add(a: i32, b: i32, c: i32) -> Result<i32, String> {
    let result = divide(a, b)?; // যদি error আসে, auto return হবে
    Ok(result + c)
}

ব্যাখ্যা:

  • ? operator basically করে manual match এর কাজ।

  • Runtime-এ কোন extra cost নেই।

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

  • Nested Result handling সহজ করতে

  • Boilerplate কমাতে


শেষ কথা

  • Option<T> → value থাকতে পারে বা না থাকতে পারে

  • Result<T,E> → function success বা failure

  • match → সব case safely handle করতে

  • ? → error automatic propagate করতে

Memory-safe:

  • Rust compile-time check করে

  • Null pointer, dangling pointer, double free কখনো হবে না

  • No garbage collector, তাই performance ঠিক থাকে

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.