This is an explicit reborrow and it's a common idiom that pops up in Rust from time to time.
&
in an expression only has one meaning: it takes an expression (which must be a place expression) of type T
and borrows a reference to it of type &T
.
For references, *
does the opposite of &
-- it takes a reference (&T
) and makes a place expression of type T
. But *
can mean different things with different kinds of pointers, since you can override it by implementing Deref
. Because *
ties in some compiler magic that automatically dereferences the return value of Deref::deref
, you can borrow the result of *
, turning it back into a plain reference, by using the &
operator.
So &*foo
is a way of explicitly reborrowing "any kind of pointer to T
" as a &T
, and is the equivalent of manually calling Deref::deref(&foo)
.
(The above explanation also works for &mut
borrows -- just replace &
with &mut
and Deref
with DerefMut
.)
It's not clear in the example you link what entities
is, but it's probably some kind of smart pointer, where the join()
method requires a plain reference. For another example where this is required, consider using [&str]::concat
to concatenate a String
with some &str
s:
// I want to concatenate this with some other strings
let s = String::from("Amelia");
// The following won't compile: you can't make an array of &str and String
assert_eq!(["Hello", ", ", s].concat(), "Hello, Amelia"); // WRONG
// However, &*s takes a reference to the str pointed to by s.
assert_eq!(["Hello", ", ", &*s].concat(), "Hello, Amelia"); // OK
See also