3 ms·
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)?
by Peaker 12y 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)?
- pcwalton 12y agoNo, that results in an error. Mutability doesn't inherit through references (`&`).
- coderjames 12y ago[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.
- FreeFull 12y agoThat 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.