What does &var mean in function parameter?

Say for function fn foo(a: T1, b: &T2, &c: &T3) {}, I know that a is passed by value, b is passed by reference.

Then what does c do?

And, in the implenmentation of foo, *b is the type of T2, c is the type of T3. But they seems having difference on ownership handling.

The parameter type is all you need to know how it is passed – &T3 is passed by reference.

The names you bind are an implementation detail, and in fact are patterns. So &c is a destructuring pattern to extract the value behind the reference. It must be a Copy type, because you’re not allowed to move out of references.

3 Likes

Thanks. So leads to the second question, what is the difference between *b and c?

Do you understand the difference between a and *b? In the body of the function, a and c behave the same way.

1 Like

I see. I thought c is more like dereferencing. Since it is a pattern, then I can see why c is same as a. Thanks.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.