I have read the rfc 2005, knowing the process of manipulation is a repeated operation. And I say encounters reference pattern
, I am not talking about encountering reference pattern at the first iteration like the following one:
let x = &String;
// The binding mode is `move`, so x is of type `&String`
But some cases where binding mode
is shifted to ref/ref mut
during the previous non-reference pattern
iteration, and then encounters a reference pattern
.
I have read the rfc very carefully, and I found this sentence The *default binding mode* only changes when matching a reference with a non-reference pattern.
, does this mean reference pattern
will do nothing to the matching process, but someone told me reference pattern
will reset the binding mode back to the default move
(see case 2).
There are two cases where the "correct" inferences seem conflict with each other:
Correct means that the inferred type is the same as the one inferred by the compiler, but I don't know if the inferring process is also correct, which is the reason why I post this question.
// case 1
let (a, b) = &(&String, &String); // a and b are of type &&String
// case 2
let (&a, &b) = &(&String, &String); // a and b are of type String
Inference for case 1:
First, we are matching &(&String, &String)
against (a, b)
, and (a, b)
is non-reference pattern
, so we deref &(&String, &String)
and set binding mode
from move(default one)
to ref
. Then we are matching (&String, &String)
against (a, b)
, with ref
binding mode, a
and b
are of type &&String
.
Why did it stop at matching
(&String, &String)
against(a, b)
, shouldn't we continue to match&String
againsta
andb
respectively?If we continue, matching
&String
againsta
,a
isreference pattern
, what should we do now?
Inference for case 2:
First, we are matching &(&String, &String)
against (&a, &b)
, (&a, &b)
is non-reference pattern
, so we deref &(&String, &String)
and set binding mode
to ref
. Then we match (&String, &String)
against (&a, &b)
, which is basically matching &String
against &a
, &a
is reference pattern
, so we reset binding mode back to move
, making a
of type String
.
Contradiction:
In case 2, when encounters reference pattern, what we do is to reset binding mode to the default move
. However in case 1(if we didn't stop at that point, keeping matching &String
against a
), a
is also reference pattern
, if we still reset binding mode to move, then a will have type &String
instead of &&String
.
And there is another question, which is when should this inferring algorithm stop
? In case 1, if the algorithm should stop at that point, then everything makes senses.