Rust tutorials for Python DEV: Variables and Mutability in Rust
Published: 2024-07-14 04:28:27

# Beginner's Guide to Variables and Mutability in Rust

In Rust, the `let` keyword is used to declare variables. When you declare a variable with `let`, you are introducing a new binding (or name) that associates with a value of a particular type.
 Here's a detailed explanation of how to assign variables using `let` in Rust:

### Basic Variable Assignment

To assign a variable in Rust, you use the `let` keyword followed by the variable name and optionally a type annotation (`: type`) if you want to explicitly specify the type.

#### Example:

```rust
fn main() {
    // Declare a variable named `x` and assign it the value `5`
    let x = 5;
    
    // Print the value of `x`
    println!("The value of x is: {}", x);
}
```

In this example:
- `let x = 5;` declares a variable named `x` and assigns it the value `5`.
- Rust infers the type of `x` as `i32` (a 32-bit signed integer) based on the assigned value.

### Explicit Type Annotation

If you want to explicitly specify the type of a variable, you can do so by appending `: type` after the variable name.

#### Example:

```rust
fn main() {
    // Declare a variable named `y` with type annotation `i64` (a 64-bit signed integer)
    let y: i64 = 100;
    
    // Print the value of `y`
    println!("The value of y is: {}", y);
}
```

In this example:
- `let y: i64 = 100;` declares a variable named `y` with the type annotation `i64` and assigns it the value `100`.

### Rebinding Variables with `let`

In Rust, variables are immutable by default. Once you assign a value to a variable, you cannot reassign it. However, you can rebind a variable by using the `let` keyword again, which shadows the previous binding.

#### Example:

```rust
fn main() {
    let z = 10;
    println!("The value of z is: {}", z);  // Output: The value of z is: 10
    
    let z = "hello";
    println!("The new value of z is: {}", z);  // Output: The new value of z is: hello
}
```

In this example:
- `let z = 10;` declares a variable `z` and assigns it the value `10`.
- Later, `let z = "hello";` shadows the previous `z` variable with a new binding of type `&str` (a string slice).

## mut
In Rust, variables are by default immutable, meaning once you assign a value to a variable, you cannot change it. However, you can explicitly make variables mutable using the `mut` keyword.
Immutable variables are declared using the `let` keyword without `mut`. Once assigned, their value cannot be changed.

**Python:**

```python
x = 5
x = 10  # This is allowed
```

**Rust:**

```rust
fn main() {
    let x = 5;
    // x = 10; // Uncommenting this line will cause a compile-time error

    println!("The value of x is: {}", x);
}
```

### Breaking Down the Code

1. **Declare an Immutable Variable**:
   ```rust
   let x = 5;
   ```
   This declares a variable `x` with the value `5`. Rust infers the type of `x` as `i32` (32-bit signed integer) in this case.

2. **Trying to Reassign the Variable**:
   ```rust
   // x = 10; // Uncommenting this line will cause a compile-time error
   ```
   Rust does not allow reassigning `x` because it's immutable.

## Mutable Variables

If you want to change the value of a variable, you need to declare it as mutable using the `mut` keyword.

**Python:**

```python
x = 5
x = 10  # This is allowed
```

**Rust:**

```rust
fn main() {
    let mut y = 5;
    y = 10;  // This is allowed because y is mutable
    println!("The value of y is: {}", y);
}
```

### Breaking Down the Code

1. **Declare a Mutable Variable**:
   ```rust
   let mut y = 5;
   ```
   This declares a mutable variable `y` with the initial value `5`.

2. **Assign a New Value**:
   ```rust
   y = 10;
   ```
   Since `y` is mutable, you can reassign its value to `10`.

## Why Use Mutability?

In Rust, immutability by default helps prevent unintended changes to data, which can help catch bugs at compile time. Mutable variables are useful when you need to change the value of a variable after it's initially set.

## Conclusion

Understanding variables and mutability in Rust is fundamental to writing safe and efficient code. By default, variables are immutable, and you can make them mutable by using the `mut` keyword. This ensures that your code is more predictable and less prone to bugs related to unintended changes. Happy coding in Rust!

This tutorial introduces the concept of variables and mutability in Rust, highlighting how Rust's default immutability helps ensure safer and more predictable code.