Skip to main content

Posts

Showing posts from April, 2026

CHAT GPT
MUTABLE and IMMUTABLE in RUST

🦀 Rust Memory Safety Mutability  ·  Borrowing  ·  Lifetimes  ·  The Borrow Checker rustc # Mutability in Rust In Rust, mutability controls whether a variable's value can change after it is created. By default, Rust is immutable — a key part of its safety model. ▸ 🔒 Immutable (Default) By default, variables in Rust cannot be changed after they are assigned. ❌ Compile error — x is immutable fn main () { let x = 5 ; x = 10 ; // ❌ ERROR: cannot assign twice to immutable variable } 👉 Here, x is immutable, so trying to change it causes a compile-time error. ▸ 🔓 Mutable ( mut keyword) To allow a variable to change, you must explicitly declare it as mutable using mut . ✅ OK — x is mutable fn main () { let mut x = 5 ; x = 10 ; // ✅ OK println! ( "{}" , x); } Feature Immutable (let) Mutable (let mut) Default? ✅ Yes ❌ No Can change...