0

My apologies if this has been asked elsewhere. This is a very simple question but I have yet to find an answer to. Consider this:

let &mut x = &mut "hello";

The compiler tells me that x is of type &str. I was expecting it to be a &mut &mut &str type or a &mut &str type. Did the compiler cancel out the &mut from both side? This was a surprise to me. Similarity, this x also ends up being a &str type:

let x = &mut "hello";
let &mut x = x;

I thought maybe the compiler don't allow &mut &mut <type> types but this is not true, the following works:

let mut x = &mut "hello";
let x = &mut x;

I'm somewhat confused by the placement of &mut between the left and right side.

bli00
  • 2,215
  • 2
  • 19
  • 46
  • 1
    `let &mut` is deconstructing the `&str` from the `&mut &str` from the right side. Its pattern matching much like `let (a,b) = (1,2);` – pigeonhands Feb 03 '23 at 01:55

0 Answers0