Introduction
Implementing the Deref
trait allows you to customize the behavior of
the dereference operator, * (as opposed to the multiplication or glob
operator). By implementing Deref
in such a way that a smart pointer
can be treated like a regular reference, you can write code that
operates on references and use that code with smart pointers too:
Box
A Box<T> can also be used:
Deref Trait
Deref Coercion
Deref coercion is a convenience that Rust performs on arguments to
functions and methods. Deref coercion works only on types that implement
the Deref trait. Deref coercion converts such a type into a reference to
another type. For example, deref coercion can convert &String
to
&str
because String
implements the Deref
trait such that it
returns str
. Deref coercion happens automatically when we pass a
reference to a particular type’s value as an argument to a function or
method that doesn’t match the parameter type in the function or method
definition. A sequence of calls to the deref
method converts the type
we provided into the type the parameter needs.