Part 01 · Rust Is Not Scary
Variables and mut
Rust এ variable বদলায় না, যতক্ষণ না তুমি mut দিয়ে বলো। এই ছোট rule টা কীভাবে অনেক রকম bug থেকে বাঁচায়।
⏱ 9 মিনিট · Station 5 / 75
The story
যেকোনো দোকানে ঢুকে দুই ধরনের label খেয়াল করো। বিস্কুটের প্যাকেটে ছাপানো দাম কখনো বদলায় না: factory ৳20 ছাপিয়ে দিয়েছে, ব্যস। আর counter এর পাশের whiteboard এ লেখা "আজকের ডিম: ডজন ৳150", দাম বদলালেই মুছে নতুন করে লেখা হয়।
Rust তোমাকে দিয়ে বলিয়ে নেয় প্রতিটা value কোন ধরনের label। Default এ প্রতিটা variable হলো ছাপানো label: একবার set করলে ওটাই থাকে। Whiteboard চাইলে mut শব্দটা দিয়ে চেয়ে নিতে হয়। শুনতে বিরক্তিকর লাগে, কিন্তু এই ছোট rule টাই পুরো এক family র bug আটকে দেয়।
The idea
Variable একটা value কে একটা নাম দেয়। Rust এ বানাও let দিয়ে:
let price = 420;
Variable নিয়ে যা কিছু করবে, তিনটা tool এই সব cover হয়:
let: এমন value যেটা বদলায় না। এটাই default, আর real Rust code এর বেশিরভাগ variable এরকমই।let mut: যে value বদলানোর plan আছে, যেমন একটা running total।mutহলো mutable এর short, মানে "বদলানো যায়"।- Shadowing: একই নামে আরেকটা
let। এটা একটা নতুন variable বানায়, যেটা পুরনোটাকে আড়াল করে দেয়। Text কে number বানাতে চাইলে, নাম টা ঠিক রেখে, এভাবেই করো।
আর একজন আত্মীয়: const, এমন value যেটা সারাজীবন fixed, আর program চলার আগেই জানা, যেমন delivery fee। Constant লেখা হয় CAPITALS এ, আর type সবসময় দিতে হয়।
Try it: a running total
Cart এ item ঢুকলে total বদলায়, তাই ওটার mut লাগবে। Delivery fee কখনো বদলায় না, তাই ওটা const:
const DELIVERY_FEE: u32 = 60;
fn main() {
let rice = 420;
let oil = 185;
let mut total = 0;
total = total + rice;
println!("after rice: {total}");
total += oil; // short for total = total + oil
println!("after oil: {total}");
total += DELIVERY_FEE;
println!("with delivery: {total}");
}
after rice: 420
after oil: 605
with delivery: 665
rice আর oil কখনো বদলায় না, তাই ওরা সাধারণ let। শুধু total বদলায়, তাই শুধু total ই mut। Code টা যে পড়বে, এক নজরে বুঝবে কোন value নড়ে আর কোনটা নড়ে না।
এবার shadowing। একজন customer type করে কয় ডজন ডিম চায়। Keyboard থেকে পাও text ("3"), কিন্তু গুণ করতে লাগবে number:
fn main() {
let quantity = "3"; // what the customer typed: text
let quantity: u32 = quantity.parse().expect("not a number");
// same name, new variable, now a number
let total = quantity * 150;
println!("{quantity} dozen eggs cost {total} taka");
let total = total + 60; // shadow again: add delivery
println!("with delivery: {total}");
}
দ্বিতীয় let total = total + 60; run হলো। প্রথম total টার কী হলো?
3 dozen eggs cost 450 taka
with delivery: 510
খেয়াল করো, প্রথম shadow টা এমনকি type ও বদলে দিয়েছে: quantity শুরু হয়েছিল text হিসেবে, হয়ে গেছে u32 number। mut দিয়ে এটা পারতে না। একটা mut variable সারাজীবন নিজের type ধরে রাখে।
What the compiler says
ভুলে যাও যে let মানে "ছাপানো label", আর দাম টা বদলানোর চেষ্টা করো:
fn main() {
let price = 420;
println!("price: {price}");
price = 450; // the shop raises the price
println!("new price: {price}");
}
error[E0384]: cannot assign twice to immutable variable `price`
--> src/main.rs:4:5
|
2 | let price = 420;
| ----- first assignment to `price`
3 | println!("price: {price}");
4 | price = 450; // the shop raises the price
| ^^^^^^^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
2 | let mut price = 420;
| +++
For more information about this error, try `rustc --explain E0384`.
error: could not compile `immutable` (bin "immutable") due to 1 previous error
error[E0384]: cannot assign twice to immutable variable `price`:priceকে তুমি একবার value দিয়েছ, আবার আরেকটা দিতে চাইছ।- Line 2 এর নিচের
-----দেখায় প্রথম assignment, আর line 4 এর নিচের^^^^দেখায় দ্বিতীয়টা। Problem এর দুই মাথাই compiler দেখিয়ে দিচ্ছে। help: consider making this binding mutable: সত্যিই বদলাতে চাইলেmutadd করো।+++ঠিক জায়গাটা দেখায়।
তবে help নেওয়ার আগে একটু ভাবো। price বদলানোই কি plan ছিল? Real shop এ যে order already place হয়ে গেছে তার দাম বদলানো উচিত না। তাহলে হয়তো ঠিক fix হলো একটা নতুন variable, let new_price = 450;, mut না। Compiler quick fix টা দেখায়, কিন্তু code এর মানে কী হবে সেটা decide করো তুমি।
একই rule list এর মতো জিনিসকেও protect করে। mut না দেওয়া একটা list (Vec) এ push করা যায় না:
fn main() {
let cart = Vec::new();
cart.push("rice");
println!("{cart:?}");
}
error[E0596]: cannot borrow `cart` as mutable, as it is not declared as mutable
--> src/main.rs:3:5
|
3 | cart.push("rice");
| ^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
2 | let mut cart = Vec::new();
| +++
For more information about this error, try `rustc --explain E0596`.
error: could not compile `cart` (bin "cart") due to 1 previous error
"Not declared as mutable" মানে একই কথা: তুমি না বললে Rust এ কিছুই বদলায় না।
Under the hood
"বদলায় না" কে default বানানো কেন? কারণ variable নিয়ে বেশিরভাগ bug হয় এই প্রশ্নে: কে বদলাল, আর কখন। Value বদলাতে না পারলে এই প্রশ্ন কখনো করতেই হয় না। 200 line এর একটা function এর শুরুতে let price = 420; পড়লে জানো, একদম শেষেও এটা 420 ই। মাঝের 200 line পড়ার দরকার নেই।
Compiler উল্টো দিক থেকেও কাজ করে। mut লিখে যদি value কখনো না বদলাও, warning দেয়। তাই mut এর একটা মানে থাকে:
fn main() {
let mut price = 420;
println!("price: {price}");
}
warning: variable does not need to be mutable
--> src/main.rs:2:9
|
2 | let mut price = 420;
| ----^^^^^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` (part of `#[warn(unused)]`) on by default
Warning error না: program তাও build হয়। এটা compiler এর কথা, "এটা চলবে, কিন্তু সম্ভবত তুমি এটা বোঝাওনি"।
আরও তিনটা detail জানা থাকা ভালো:
- Shadowing এর runtime এ কোনো খরচ নেই। নতুন value এর জন্য শুধু একটা নতুন নাম। Optimize করা build এ compiler চাইলে একই memory আবার use করতে পারে।
constআসলে variable ই না। যেখানে যেখানে use হয়েছে, compiler সেখানে সেখানে ওর value বসিয়ে দেয়। তাই program চলার সময় memory তেDELIVERY_FEEনামে কোনো box থাকে না।- Use করার আগে
letএ value থাকতেই হবে। যে variable এ হয়তো এখনো value নেই, Rust সেটা read করতে দেয় না। তাই JavaScript এর মতো "undefined" এর কোনো ব্যাপার নেই।
Compared with other languages
JavaScript এ const আছে, দেখতে Rust এর let এর মতো। কিন্তু ঠিক এক না:
const cart = [];
cart.push("rice"); // allowed: const only locks the name, not the array
console.log(cart);
cart = ["oil"]; // not allowed: this changes what the name points to
[ 'rice' ]
/home/you/rust-lab/cart.js:4
cart = ["oil"]; // not allowed: this changes what the name points to
^
JavaScript এর const শুধু নাম টা lock করে: cart সবসময় একই array কে point করবে, কিন্তু সেই array তে যে কেউ push করতে পারে। Rust এর সাধারণ let value টাও lock করে, তাই উপরে cart.push fail করেছিল। আর খেয়াল করো কোন error কখন আসে: JavaScript আগে [ 'rice' ] print করেছে, তারপর চলতে চলতে line 4 এ fail করেছে। Rust কিছু run হওয়ার আগেই fail করেছে।
| Python | JavaScript | Go | Rust | |
|---|---|---|---|---|
| Default | সব বদলায় | let বদলায়, const নাম lock করে | সব বদলায় | কিছুই বদলায় না |
| Value বদলানো | সোজা assign | let | সোজা assign | let mut |
| সারাজীবন fixed | নিয়ম করে CAPS | const (শুধু নাম) | const | const |
| একই নাম, নতুন value | সোজা assign | let দিয়ে allowed না | নতুন block এ := | shadowing |
Key takeaway
Rust এ mut না বললে variable বদলায় না। যে value থেকে যায় তার জন্য সাধারণ let, running total আর counter এর জন্য let mut, একটা value কে নতুন কিছুতে বদলাতে shadowing (একই নামে আবার let), আর delivery fee এর মতো fixed number এর জন্য const। Compiler দুই দিক থেকেই এটা মানায়: সাধারণ let বদলাতে গেলে error, অদরকারি mut দিলে warning। তাই Rust code এ mut একটা signal: "এটার দিকে চোখ রাখো, এটা নড়ে"।
Practice
Objective: একটা ছোট program এ let, let mut, shadowing আর const সব use করো।
Challenge: তিনটা item এর bill বানাও (চাল ৳420, তেল ৳185, ডিম ৳150), একটা mut total দিয়ে। তারপর total কে shadow করে 10% ছাড় দাও (integer হিসাব চলবে), আর আবার shadow করে ৳60 এর একটা const DELIVERY_FEE যোগ করো। প্রতিটা step এর পর total print করো।
Expected result:
items: 755
after 10%: 680
to pay: 740
Hint 1
const DELIVERY_FEE: u32 = 60; যাবে fn main এর উপরে, আর type লাগবে।
Hint 2
let mut total = 0; দিয়ে শুরু করো, আর প্রতিটা item total += ...; দিয়ে যোগ করো।
Hint 3
10% ছাড় মানে total - total / 10। এটাকে let total = total - total / 10; লেখো, তাহলে এটা একটা নতুন total, আর ওর mut লাগে না।
Solution
const DELIVERY_FEE: u32 = 60;
fn main() {
let mut total = 0;
total += 420; // rice 5kg
total += 185; // soybean oil
total += 150; // eggs (12)
println!("items: {total}");
let total = total - total / 10; // 10% off, a new `total` shadows the old one
println!("after 10%: {total}");
let total = total + DELIVERY_FEE;
println!("to pay: {total}");
}
679.50 না হয়ে 680 কেন? 755 / 10 হলো 75, 75.5 না। কারণ Rust এ দুইটা পূর্ণ সংখ্যা ভাগ করলে পূর্ণ সংখ্যাই আসে। পরের lesson পুরোটাই এটা নিয়ে: কোন number type নেবে, আর সীমানায় গেলে কী হয়।
Station stamp
Chapter শেষ করো
পড়া শেষ হলে Complete চাপো, progress save হবে। XP শুধু Pro তে — Pro থাকলে এই chapter এ +10 XP পেতে।
Pro নিয়ে XP পাও →