Implementation of `Borrow<T>` for `&T` and `&mut T`

Why are the implementations of Borrow<T> for &T and &mut T like this

impl<T: ?Sized> Borrow<T> for &T {
    fn borrow(&self) -> &T { &**self }
}

impl<T: ?Sized> Borrow<T> for &mut T {
    fn borrow(&self) -> &T { &**self }
}

instead of this?

impl<T: ?Sized> Borrow<T> for &T {
    fn borrow(&self) -> &T { *self }
}

impl<T: ?Sized> Borrow<T> for &mut T {
    fn borrow(&self) -> &T { *self }
}

It appears to me that these should be the same, and at least the one for &T seems more natural (the other has an implicit conversion from &mut T to &T, right?). Am I missing something? Is it just a matter of preference or there is some difference?

Both ways of writing are equivalent. I tried to trace the impls back using git blame and ended up here, but couldn't find earlier mentions of the impl. I would guess *self would also have worked back then and assume there's no particular reason why it has to be &**self instead.

Thanks a lot!

I feared there was maybe some mysterious interaction with std::ops::Deref, with which the &** trick is sometimes used.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.