11

I encountered this block of code while reading documentation from a Rust library:

for (ent, pos, vel) in (&*entities, &mut pos_storage, &vel_storage).join() {
    println!("Processing entity: {:?}", ent);
    *pos += *vel;
}

What does &*entities do here? From what I can tell it’s dereferencing entities and then referencing it again. But why?

kmdreko
  • 42,554
  • 6
  • 57
  • 106
Tien Nguyen
  • 113
  • 1
  • 5

1 Answers1

12

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 &strs:

// 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

trent
  • 25,033
  • 7
  • 51
  • 90