I was reading the docs for Rust's Deref
trait:
pub trait Deref {
type Target: ?Sized;
fn deref(&self) -> &Self::Target;
}
The type signature for the deref
function seems counter-intuitive to me; why is the return type a reference? If references implement this trait so they can be dereferenced, what effect would this have at all?
The only explanation that I can come up with is that references don't implement Deref
, but are considered "primitively dereferenceable". However, how would a polymorphic function which would work for any dereferenceable type, including both Deref<T>
and &T
, be written then?
You can see all the types that implement
Deref
, and&T
is in that list:The non-obvious thing is that there is syntactical sugar being applied when you use the
*
operator with something that implementsDeref
. Check out this small example:The explicit call to
deref
returns a&str
, but the operator*
returns astr
. It's more like you are calling*Deref::deref(&s)
, ignoring the implied infinite recursion.Xirdus is correct in saying
Although "useless" is a bit strong; it would still be useful for types that implement
Copy
.See also:
The compiler knows only how to dereference &-pointers - but it also knows that types that implement
Deref
trait have aderef()
method that can be used to get an appropriate reference to something inside given object. If you dereference an object, what you actually do is first obtain the reference and only then dereference it.If
deref()
returned a value, it would either be useless because it would always move out, or have semantics that drastically differ from every other function which is not nice.