What happens when pattern-matching against a reference with a pattern that doesn't include a reference?
Here's an example using a struct pattern:
fn main() {
struct S(u32);
let S(x) = &S(2);
// type of x is `&u32`
}
The behavior is surprising to me because the pattern on the left does not seem to match the data on the right, unlike let &S(x) = &S(2)
where the &
s line up.
It looks like what is happening is that when the RHS is a struct reference and the lhs is a struct pattern with field patterns, the type of the variable in the field pattern is &F
where F
is the type of the field.
What I am looking for is:
- a reference that explains what the expected behavior is
- an explanation of what the behavior is that is general enough to explain what happens with tuples and enums in addition to structs. For example, in
let (x,) = &(2,);
the type ofx
isi32
(correction:&i32
).
I couldn't find anything about this in the Rust Reference or the Rust Book, but I may have missed it.