Hacker News new | ask | show | jobs
by Peaker 4420 days ago
I was a bit surprised by the reverse:

  let x = 5
  let mut y = &x
If I understood correctly, x is immutable, but can still be mutated via (*y)?
3 comments

No, that results in an error. Mutability doesn't inherit through references (`&`).
[disclaimer: still learning Rust]

I believe it actually means that y can be made to reference something other than x.

  let x = 5
  let mut y = &x
  let i = 13
  y = &i
This is similar to C++'s const-pointers and pointers-to-consts, where either a pointer cannot be made to point at something different or the pointer cannot be used to change what it points at.
That actually lets you change what y points at, but you can't mutate x through y. If you want to mutate x, you need something like

    let y = &mut x;
which will result in an error due to x not being mutable.