use std::ops::Deref;
fn main() {
let box_people = Box::new(People {
name: String::from("test"),
});
let _p = *box_people; // move the ownership of box_people to p
let box_people = Box::new(People {
name: String::from("test"),
});
// let _p = *(box_people.deref()); // cannot compile
}
#[derive(Debug)]
struct People {
name: String,
}
Hi guys,
in the book, the rust compile will operate *Box<T> as *(Box<T>.deref()).
I have checked the deref method actually only provides the borrow of T, which means if T without Copy trait cannot move the ownership of shared reference. So if let _p = *box_people is equal to let _p=*(box_people.deref()), it will not make sense to Rust's borrow check. In fact, rust compiler throw a error, when I try let _p=*(box_people.deref()). So now I don't understand why let _p = *box_people can work and the data in box is also moved out to a new variable.
No, as a special case, this is not true for a couple of types, including Box. Dereferencing Boxes (and also regular references) doesn't go through the Deref impl.
Thank you so much for your reply. I am curious about how to gain a better understanding of this inner mechanism. If it's not too much trouble, could you provide me with a guide? Your help is greatly appreciated.
I apologize for not being clear earlier. My question is how can I gain insight into the internal workings of the Rust compiler for the special case you mentioned.