FAQ - Help and questions

How do I fix "cannot borrow as mutable" and "cannot assign twice to immutable variable"?

#errors#mutability#basicspublished 2 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail
Last post

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.

Checked with rustc 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

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.

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    2 answers from the Ruststudio team: different approaches, each with its own trade-offs

    Sort: Oldest Newest
    #2

    Changing through a method (E0596)#

    src/main.rsrust
    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:

    src/main.rsrust
    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).

      QuoteReply
      Share
      Hacker NewsRedditLinkedInE-mail
      #3

      Assigning again (E0384)#

      src/main.rsrust
      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`.
      src/main.rsrust
      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.
        QuoteReply
        Share
        Hacker NewsRedditLinkedInE-mail