Reputation: 129
fn main () {
let x: &Option<_> = &Some(90);
match x {
// no ref
&Some(y) => {
print!("{}", y);
},
&None => {
},
}
match x {
// ref
&Some(ref y) => {
print!("{}", y);
},
&None => {
},
}
}
// What's the difference between the two?
Upvotes: 0
Views: 679
Reputation: 23556
ref
is inverse of &
on the left side of match. In other words:
let ref a = x;
Is the same as
let a = &x;
The reason for that syntax is that with structural matching it is not always possible to use 2nd form:
struct Foo(usize);
let Foo(ref a) = x;
Upvotes: 4