# How do I fix "cannot borrow as mutable" and "cannot assign twice to immutable variable"?
Source: https://ruststudio.org/community/t/141/how-do-i-fix-cannot-borrow-as-mutable-and-cannot-assign-twice-to-immutable
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: errors, mutability, basics | Views: 0

## Short answer
Variables are immutable by default. To change a value, declare it with let mut. For a parameter you want to change through a reference, the parameter must be &mut T and the caller must pass &mut value. The two messages (E0596 and E0384) both point at the declaration that needs mut.

## Answer
## Answer
Rust makes you say where a value can change, so that readers (and the compiler) know everything else cannot. Two messages cover most cases:

- **E0596**, "cannot borrow `x` as mutable, as it is not declared as mutable": you called a method that changes the value (`push`, `insert`, `clear`) on a variable without `mut`.
- **E0384**, "cannot assign twice to immutable variable": you assigned to a variable (`x = ...`, `x += 1`) that was declared without `mut`.

The answers below show one of each.

## Answers (2)

### Answer 1 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Changing through a method (E0596)

```rust title=src/main.rs
fn main() {
    let numbers = Vec::new();
    numbers.push(1);
    println!("{:?}", numbers);
}
```

```text
error[E0596]: cannot borrow `numbers` as mutable, as it is not declared as mutable
 --> src/main.rs:3:5
  |
3 |     numbers.push(1);
  |     ^^^^^^^ cannot borrow as mutable
  |
help: consider changing this to be mutable
  |
2 |     let mut numbers = Vec::new();
  |         +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0596`.
```

Add `mut`, as the help text suggests:

```rust title=src/main.rs
fn main() {
    let mut numbers = Vec::new();
    numbers.push(1);
    println!("{:?}", numbers);
}
```

```text
[1]
```

If the value arrives as a parameter, `mut` on the parameter is not enough to change the caller's value. Write the parameter as `v: &mut Vec<i32>` and call `f(&mut numbers)`.

### Answer 2 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Assigning again (E0384)

```rust title=src/main.rs
fn main() {
    let count = 0;
    for _ in 0..3 {
        count += 1;
    }
    println!("{count}");
}
```

```text
error[E0384]: cannot assign twice to immutable variable `count`
 --> src/main.rs:4:9
  |
2 |     let count = 0;
  |         ----- first assignment to `count`
3 |     for _ in 0..3 {
4 |         count += 1;
  |         ^^^^^^^^^^ cannot assign twice to immutable variable
  |
help: consider making this binding mutable
  |
2 |     let mut count = 0;
  |         +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0384`.
```

```rust title=src/main.rs
fn main() {
    let mut count = 0;
    for _ in 0..3 {
        count += 1;
    }
    println!("{count}");
}
```

```text
3
```

If you only want a new value for the next steps (not a change in a loop), shadowing is another option: `let count = count + 1;` creates a new variable.

## Common mistakes
- Putting `mut` on everything. Add it where the compiler asks; a variable without `mut` tells the reader it never changes.
- Writing `&mut` where a plain `&` is enough. A read-only function should take `&`, so callers can pass the same value to several of them.
