@@ -93,9 +93,37 @@ Associated functions whose first parameter is named `self` are called *methods*
93
93
and may be invoked using the [ method call operator] , for example, ` x.foo() ` , as
94
94
well as the usual function call notation.
95
95
96
- If the type of the ` self ` parameter is specified, it is limited to the type
97
- being implemented (or ` Self ` ), or a reference or mutable reference to the
98
- type, or a boxed value of the type being implemented (such as ` Box<Self> ` ).
96
+ If the type of the ` self ` parameter is specified, it is limited to one of the
97
+ following types:
98
+
99
+ - ` Self `
100
+ - ` &Self `
101
+ - ` &mut Self `
102
+ - [ ` Box<Self> ` ]
103
+ - [ ` Rc<Self> ` ]
104
+ - [ ` Arc<Self> ` ]
105
+ - [ ` Pin<P> ` ] where ` P ` is one of the above types except ` Self ` .
106
+
107
+ The ` Self ` term can be replaced with the type being implemented.
108
+
109
+ ``` rust
110
+ # use std :: rc :: Rc ;
111
+ # use std :: sync :: Arc ;
112
+ # use std :: pin :: Pin ;
113
+ struct Example ;
114
+ impl Example {
115
+ fn by_value (self : Self ) {}
116
+ fn by_ref (self : & Self ) {}
117
+ fn by_ref_mut (self : & mut Self ) {}
118
+ fn by_box (self : Box <Self >) {}
119
+ fn by_rc (self : Rc <Self >) {}
120
+ fn by_arc (self : Arc <Self >) {}
121
+ fn by_pin (self : Pin <& Self >) {}
122
+ fn explicit_type (self : Arc <Example >) {}
123
+ fn with_lifetime <'a >(self : & 'a Self ) {}
124
+ }
125
+ ```
126
+
99
127
Shorthand syntax can be used without specifying a type, which have the
100
128
following equivalents:
101
129
@@ -107,7 +135,17 @@ Shorthand | Equivalent
107
135
108
136
> Note: Lifetimes can be and usually are elided with this shorthand.
109
137
110
- Consider the following trait:
138
+ If the ` self ` parameter is prefixed with ` mut ` , it becomes a mutable variable,
139
+ similar to regular parameters using a ` mut ` [ identifier pattern] . For example:
140
+
141
+ ``` rust
142
+ trait Changer : Sized {
143
+ fn change (mut self ) {}
144
+ fn modify (mut self : Box <Self >) {}
145
+ }
146
+ ```
147
+
148
+ As an example of methods on a trait, consider the following:
111
149
112
150
``` rust
113
151
# type Surface = i32 ;
@@ -294,11 +332,16 @@ fn main() {
294
332
[ _Lifetime_ ] : trait-bounds.html
295
333
[ _Type_ ] : types.html#type-expressions
296
334
[ _WhereClause_ ] : items/generics.html#where-clauses
335
+ [ `Arc<Self>` ] : special-types-and-traits.html#arct
336
+ [ `Box<Self>` ] : special-types-and-traits.html#boxt
337
+ [ `Pin<P>` ] : special-types-and-traits.html#pinp
338
+ [ `Rc<Self>` ] : special-types-and-traits.html#rct
297
339
[ trait ] : items/traits.html
298
340
[ traits ] : items/traits.html
299
341
[ type aliases ] : items/type-aliases.html
300
342
[ inherent implementations ] : items/implementations.html#inherent-implementations
301
343
[ identifier ] : identifiers.html
344
+ [ identifier pattern ] : patterns.html#identifier-patterns
302
345
[ trait object ] : types/trait-object.html
303
346
[ implementations ] : items/implementations.html
304
347
[ type ] : types.html#type-expressions
0 commit comments