I'm struggling with destructuring/auto dereferencing.
I have a snippet of code, which works and I kind of see why - a
is mutable and we pass a mutable reference to match
. In the match
statement we have a pattern which expects a mutable reference, and we take inner variables which are references to avoid moving them out.
let x = vec![1, 2];
let y = vec![3, 4];
let mut a = (x, y);
match &mut a {
&mut (ref aa, ref bb) => {}
}
I'm struggling to see why the following pattern works. I expect the pattern not to match to begin with, but it matches and aa
and bb
are both mutable references (no need for ref mut
). I feel that this is some auto dereferencing at play:
match &mut a {
(aa, bb) => {
aa[0] = 2;
}
}