Can a RC move location behind my back?

This is a followup to Hash based on address (not value) of Rc

let x : Rc<...>

is there any guarantee that &*x remains constant ?

Intuitively, I want to say “yes” … however, if that was the case, why does ‘pin’ exist for rc? https://doc.rust-lang.org/std/rc/struct.Rc.html#method.pin

The data it points to will not move (the pointer handle itself can move).

Pin exists to prevent you from mem::swaping it with something or from taking the data out with try_unwrap.

2 Likes

In practice, Rc::make_mut and Rc::get_mut (EDIT: and try_unwrap) are the main reason why Rc<Pointee> and Pin<Rc<Pointee>> are not equivalent (those methods may “move location”). Otherwise, they are equivalent API-wise, since they both Deref<Target = Pointee>.

Also, see this thread

2 Likes