1 /// Used for immutable dereferencing operations, like `*v`.
3 /// In addition to being used for explicit dereferencing operations with the
4 /// (unary) `*` operator in immutable contexts, `Deref` is also used implicitly
5 /// by the compiler in many circumstances. This mechanism is called
6 /// ['`Deref` coercion'][more]. In mutable contexts, [`DerefMut`] is used.
8 /// Implementing `Deref` for smart pointers makes accessing the data behind them
9 /// convenient, which is why they implement `Deref`. On the other hand, the
10 /// rules regarding `Deref` and [`DerefMut`] were designed specifically to
11 /// accommodate smart pointers. Because of this, **`Deref` should only be
12 /// implemented for smart pointers** to avoid confusion.
14 /// For similar reasons, **this trait should never fail**. Failure during
15 /// dereferencing can be extremely confusing when `Deref` is invoked implicitly.
17 /// # More on `Deref` coercion
19 /// If `T` implements `Deref<Target = U>`, and `x` is a value of type `T`, then:
21 /// * In immutable contexts, `*x` on non-pointer types is equivalent to
22 /// `*Deref::deref(&x)`.
23 /// * Values of type `&T` are coerced to values of type `&U`
24 /// * `T` implicitly implements all the (immutable) methods of the type `U`.
26 /// For more details, visit [the chapter in *The Rust Programming Language*][book]
27 /// as well as the reference sections on [the dereference operator][ref-deref-op],
28 /// [method resolution] and [type coercions].
30 /// [book]: ../../book/ch15-02-deref.html
31 /// [`DerefMut`]: trait.DerefMut.html
32 /// [more]: #more-on-deref-coercion
33 /// [ref-deref-op]: ../../reference/expressions/operator-expr.html#the-dereference-operator
34 /// [method resolution]: ../../reference/expressions/method-call-expr.html
35 /// [type coercions]: ../../reference/type-coercions.html
39 /// A struct with a single field which is accessible by dereferencing the
43 /// use std::ops::Deref;
45 /// struct DerefExample<T> {
49 /// impl<T> Deref for DerefExample<T> {
52 /// fn deref(&self) -> &Self::Target {
57 /// let x = DerefExample { value: 'a' };
58 /// assert_eq!('a', *x);
63 #[stable(feature = "rust1", since = "1.0.0")]
65 /// The resulting type after dereferencing.
66 #[stable(feature = "rust1", since = "1.0.0")]
69 /// Dereferences the value.
71 #[stable(feature = "rust1", since = "1.0.0")]
72 fn deref(&self) -> &Self::Target;
75 #[stable(feature = "rust1", since = "1.0.0")]
76 impl<T: ?Sized> Deref for &T {
79 fn deref(&self) -> &T {
84 #[stable(feature = "rust1", since = "1.0.0")]
85 impl<T: ?Sized> Deref for &mut T {
88 fn deref(&self) -> &T {
93 /// Used for mutable dereferencing operations, like in `*v = 1;`.
95 /// In addition to being used for explicit dereferencing operations with the
96 /// (unary) `*` operator in mutable contexts, `DerefMut` is also used implicitly
97 /// by the compiler in many circumstances. This mechanism is called
98 /// ['`Deref` coercion'][more]. In immutable contexts, [`Deref`] is used.
100 /// Implementing `DerefMut` for smart pointers makes mutating the data behind
101 /// them convenient, which is why they implement `DerefMut`. On the other hand,
102 /// the rules regarding [`Deref`] and `DerefMut` were designed specifically to
103 /// accommodate smart pointers. Because of this, **`DerefMut` should only be
104 /// implemented for smart pointers** to avoid confusion.
106 /// For similar reasons, **this trait should never fail**. Failure during
107 /// dereferencing can be extremely confusing when `DerefMut` is invoked
110 /// # More on `Deref` coercion
112 /// If `T` implements `DerefMut<Target = U>`, and `x` is a value of type `T`,
115 /// * In mutable contexts, `*x` on non-pointer types is equivalent to
116 /// `*DerefMut::deref_mut(&mut x)`.
117 /// * Values of type `&mut T` are coerced to values of type `&mut U`
118 /// * `T` implicitly implements all the (mutable) methods of the type `U`.
120 /// For more details, visit [the chapter in *The Rust Programming Language*][book]
121 /// as well as the reference sections on [the dereference operator][ref-deref-op],
122 /// [method resolution] and [type coercions].
124 /// [book]: ../../book/ch15-02-deref.html
125 /// [`Deref`]: trait.Deref.html
126 /// [more]: #more-on-deref-coercion
127 /// [ref-deref-op]: ../../reference/expressions/operator-expr.html#the-dereference-operator
128 /// [method resolution]: ../../reference/expressions/method-call-expr.html
129 /// [type coercions]: ../../reference/type-coercions.html
133 /// A struct with a single field which is modifiable by dereferencing the
137 /// use std::ops::{Deref, DerefMut};
139 /// struct DerefMutExample<T> {
143 /// impl<T> Deref for DerefMutExample<T> {
146 /// fn deref(&self) -> &Self::Target {
151 /// impl<T> DerefMut for DerefMutExample<T> {
152 /// fn deref_mut(&mut self) -> &mut Self::Target {
157 /// let mut x = DerefMutExample { value: 'a' };
159 /// assert_eq!('b', *x);
161 #[lang = "deref_mut"]
163 #[stable(feature = "rust1", since = "1.0.0")]
164 pub trait DerefMut: Deref {
165 /// Mutably dereferences the value.
166 #[stable(feature = "rust1", since = "1.0.0")]
167 fn deref_mut(&mut self) -> &mut Self::Target;
170 #[stable(feature = "rust1", since = "1.0.0")]
171 impl<T: ?Sized> DerefMut for &mut T {
172 fn deref_mut(&mut self) -> &mut T {
177 /// Indicates that a struct can be used as a method receiver, without the
178 /// `arbitrary_self_types` feature. This is implemented by stdlib pointer types like `Box<T>`,
179 /// `Rc<T>`, `&T`, and `Pin<P>`.
181 #[unstable(feature = "receiver_trait", issue = "none")]
187 #[unstable(feature = "receiver_trait", issue = "none")]
188 impl<T: ?Sized> Receiver for &T {}
190 #[unstable(feature = "receiver_trait", issue = "none")]
191 impl<T: ?Sized> Receiver for &mut T {}