๐ฆ mutable variable โ Mutability in Rust
as i mentioned, variables are immutable by default. This means that once a value is assigned to a variable, it cannot be changed. However, sometimes you may need to modify a variable's value during execution. In such cases, you can declare the variable as mutable by using the mut keyword.
Declaring Mutable Variables
To declare a mutable variable in Rust, simply add mut before the variable name.
Example: Mutable Variables
fn main() { let mut x = 5; // mutable variable println!("x is: {}", x); // Output: x is: 5 x = 10; // changing the value of x println!("Now, x is: {}", x); // Output: Now, x is: 10 }
๐ Line-by-line breakdown:
-
let mut x = 5;โ This declares a mutable variablexand assigns it the value5. -
x = 10;โ This changes the value ofxfrom5to10.
Why Use Mutability?
-
Memory Safety: By default, Rust prevents accidental changes to variables, which helps avoid bugs and ensures that the programโs state is predictable.
-
Explicit Intent: Declaring variables as
mutmakes it clear in the code that the variable is intended to be modified. This improves code readability and reduces confusion.
Example: without Mutability
fn main() { let x = 5; // immutable variable println!("x is: {}", x); // Output: x is: 5 // x = 10; // This would cause an error: cannot assign twice to immutable variable }
๐ก Key points to remember:
-
In Rust, variables are immutable by default.
-
Use the
mutkeyword to make a variable mutable if you plan to change its value after initial assignment. -
You cannot change the value of an immutable variable.