1

Looking at implementation of Deref trait for Box:

impl<T: ?Sized, A: Allocator> const Deref for Box<T, A> {
    type Target = T;

    fn deref(&self) -> &T {
        &**self
    }
}

i cannot understand double use of dereference operator * on self. By implementing Deref trait that way on a custom type, obviously the compiler complains about a recursive call. I'm missing something...

MirkoBanchi
  • 2,173
  • 5
  • 35
  • 52
  • uhm , interesting. My guess is that Box is an indirection already, so when you have a `&Box` you have really 2 indirections, by applying one `*` you get `Box`, with the second `*` you get `T` inside `Box`, the you add `&` to get `&T` – Netwave May 13 '22 at 07:42
  • 1
    Can't yet make it into the proper answer, but here's something on the topic: https://manishearth.github.io/blog/2017/01/10/rust-tidbits-box-is-special/ – Cerberus May 13 '22 at 08:44
  • 1
    @Cerberus Very interesting article, i missed that. I don't delete my (duplicated) question only to leave your link available to others. – MirkoBanchi May 13 '22 at 17:37
  • 3
    Well, duplicates can be good too, since they provide an alternative search terms for the same problem, thus improving the chance for further readers to get their answer without waiting. – Cerberus May 13 '22 at 17:41

0 Answers0