Rust's Immutable Variables: A Game Changer for Code Quality

Day 2/90: Variables That Refuse to Change (Literally) 🔒 In Rust, variables are immutable by default. Coming from Python where everything just... changes whenever, this is wild. let x = 5; x = 6; // ❌ NOPE. Compiler says no. Want to change a variable? You have to explicitly say so: let mut y = 5; // NOW you can change it y = 6; // ✅ Works! At first I was like "why is Rust making my life harder?" But then I realized something... In my data analysis work, I've had bugs where I accidentally overwrote a variable in a loop and spent hours debugging. In Rust? The compiler would've caught that immediately. It's like Rust is forcing me to think: "Does this NEED to change?" 99% of the time, it doesn't. The craziest part? SHADOWING. You can reuse variable names AND change their type: let spaces = "  ";    // string let spaces = spaces.len(); // now it's a number! This is different from `mut` because you're creating a NEW variable, not changing the old one. Mind = blown. Real talk: This felt annoying at first, but after getting a few compiler errors, I get it. The friction is intentional. It's teaching me to write better code. Coming from "move fast and break things" languages, Rust is more like "move deliberately and don't break anything." --- 💡 TL;DR: - Variables immutable by default (use `mut` to change) - Immutability = fewer bugs in production - Shadowing lets you reuse names and change types - The compiler's strictness is actually helpful - Day 2: Trust issues in code form ✅ 🔗 Code: https://lnkd.in/eKABvh2U #RustLang #LearnInPublic #100DaysOfCode #Programming #SoftwareEngineering Do you prefer immutable-by-default or mutable-by-default languages?

  • text

To view or add a comment, sign in

Explore content categories