Explained asterisk on & and &mut reference
This commit is contained in:
parent
8f3901feab
commit
855f1ff321
1 changed files with 4 additions and 0 deletions
|
@ -125,6 +125,10 @@ This will print `6`. We make `y` a mutable reference to `x`, then add one to
|
||||||
the thing `y` points at. You’ll notice that `x` had to be marked `mut` as well,
|
the thing `y` points at. You’ll notice that `x` had to be marked `mut` as well,
|
||||||
if it wasn’t, we couldn’t take a mutable borrow to an immutable value.
|
if it wasn’t, we couldn’t take a mutable borrow to an immutable value.
|
||||||
|
|
||||||
|
You'll also notice we added an asterisk in front of `y`, making it `*y`,
|
||||||
|
this is because y is an `&mut` reference. You'll also need to use them for
|
||||||
|
accessing and modifying `&` references as well.
|
||||||
|
|
||||||
Otherwise, `&mut` references are just like references. There _is_ a large
|
Otherwise, `&mut` references are just like references. There _is_ a large
|
||||||
difference between the two, and how they interact, though. You can tell
|
difference between the two, and how they interact, though. You can tell
|
||||||
something is fishy in the above example, because we need that extra scope, with
|
something is fishy in the above example, because we need that extra scope, with
|
||||||
|
|
Loading…
Reference in a new issue