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

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 করি:
Value থাকতে পারে বা নাও থাকতে পারে →
Option<T>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 করি।না পেলে
Nonereturn করি।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)→ SuccessErr(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 করে manualmatchএর কাজ।Runtime-এ কোন extra cost নেই।
কেন ব্যবহার করি:
Nested
Resulthandling সহজ করতেBoilerplate কমাতে
শেষ কথা
Option<T>→ value থাকতে পারে বা না থাকতে পারেResult<T,E>→ function success বা failurematch→ সব case safely handle করতে?→ error automatic propagate করতে
Memory-safe:
Rust compile-time check করে
Null pointer, dangling pointer, double free কখনো হবে না
No garbage collector, তাই performance ঠিক থাকে




