A reference created with ref
is exactly the same as reference taken with &
.
The difference is where they’re allowed in the syntax. ref
on the left side of an assignment is like adding &
on the right side.
This redundancy exists because in pattern matching &
is used to require that a reference exists already, rather than to make a new one:
let foo = 1;
match foo {
ref x => {
/* x == &1 */
match x {
&y => /* y == 1 */
}
},
}
// A `ref` borrow on the left side of an assignment is equivalent to
// an `&` borrow on the right side.
let ref ref_c1 = c;
let ref_c2 = &c;