When to use ref
in Rust?
Probably only in match
and if let
if the borrow checker shouts at you. These are the same:
let ref x = y;
let x = &y;
ref
is the way of saying you want to make a reference when you can only write the left side (the pattern) of the assignment.
In patterns &
means you expect to see a reference, but it doesn't make one. These are the same:
let x = y;
let &x = &y;
Try it: Rust Playground