|
9 | 9 | > `}` |
10 | 10 | > |
11 | 11 | > _TraitItem_ :\ |
12 | | -> [_OuterAttribute_]<sup>\*</sup> (_TraitMethod_ | _TraitConst_ | _TraitType_) |
| 12 | +> [_OuterAttribute_]<sup>\*</sup> (_TraitFunc_ | _TraitMethod_ | _TraitConst_ | _TraitType_) |
| 13 | +> |
| 14 | +> _TraitFunc_ :\ |
| 15 | +> _TraitFunctionDecl_ ( `;` | [_BlockExpression_] ) |
13 | 16 | > |
14 | 17 | > _TraitMethod_ :\ |
15 | | -> [_MethodType_] `;` | [_Method_] |
| 18 | +> _TraitMethodDecl_ ( `;` | [_BlockExpression_] ) |
| 19 | +> |
| 20 | +> _TraitFunctionDecl_ :\ |
| 21 | +> [_FunctionFront_] `fn` [IDENTIFIER] [_Generics_]<sup>?</sup>\ |
| 22 | +> `(` _TraitFunctionParameters_<sup>?</sup> `)`\ |
| 23 | +> [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup> |
| 24 | +> |
| 25 | +> _TraitMethodDecl_ :\ |
| 26 | +> [_FunctionFront_] `fn` [IDENTIFIER] [_Generics_]<sup>?</sup>\ |
| 27 | +> `(` [_SelfParam_] (`,` _TraitFunctionParam_)<sup>\*</sup> `,`<sup>?</sup> `)`\ |
| 28 | +> [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup> |
| 29 | +> |
| 30 | +> _TraitFunctionParameters_ :\ |
| 31 | +> _TraitFunctionParam_ (`,` _TraitFunctionParam_)<sup>\*</sup> `,`<sup>?</sup> |
| 32 | +> |
| 33 | +> _TraitFunctionParam_<sup>[†](#parameter-patterns)</sup> :\ |
| 34 | +> ( [_Pattern_] `:` )<sup>?</sup> [_Type_] |
16 | 35 | > |
17 | 36 | > _TraitConst_ :\ |
18 | 37 | > `const` [IDENTIFIER] ( ( `:` [_Type_] ) ( `=` [_Expression_] )<sup>?</sup> )<sup>?</sup> `;` |
@@ -137,18 +156,54 @@ let nonsense = circle.radius() * circle.area(); |
137 | 156 |
|
138 | 157 | ## Unsafe traits |
139 | 158 |
|
140 | | -Traits that begin with the `unsafe` keyword indicate that *implementing* the |
| 159 | +Traits items that begin with the `unsafe` keyword indicate that *implementing* the |
141 | 160 | trait may be [unsafe]. It is safe to use a correctly implemented unsafe trait. |
142 | | -The [trait implementation] must also include the `unsafe` keyword. |
| 161 | +The [trait implementation] must also begin with the `unsafe` keyword. |
143 | 162 |
|
144 | 163 | [`Sync`] and [`Send`] are examples of unsafe traits. |
145 | 164 |
|
| 165 | +## Parameter patterns |
| 166 | + |
| 167 | +The pattern for a trait function or method parameter is optional: |
| 168 | + |
| 169 | +```rust |
| 170 | +trait T { |
| 171 | + fn f(i32); // Parameter identifiers are not required. |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +The kinds of patterns for parameters is limited to one of the following: |
| 176 | + |
| 177 | +* [IDENTIFIER] |
| 178 | +* `mut` [IDENTIFIER] |
| 179 | +* [`_`][WildcardPattern] |
| 180 | +* `&` [IDENTIFIER] |
| 181 | +* `&&` [IDENTIFIER] |
| 182 | + |
| 183 | +Function or method declarations without a body only allow [IDENTIFIER] or |
| 184 | +[wild card][WildcardPattern] patterns. `mut` [IDENTIFIER] is currently |
| 185 | +allowed, but it is deprecated and will become a hard error in the future. |
| 186 | +<!-- https://github.com/rust-lang/rust/issues/35203 --> |
| 187 | + |
| 188 | +<!-- 2018 changes: |
| 189 | +
|
| 190 | +Function or method parameter patterns are no longer optional, and are required. |
| 191 | +
|
| 192 | +All irrefutable pattern kinds are allowed (as long as there is a body). |
| 193 | +--> |
| 194 | + |
| 195 | + |
146 | 196 | [IDENTIFIER]: identifiers.html |
| 197 | +[WildcardPattern]: patterns.html#wildcard-pattern |
| 198 | +[_BlockExpression_]: expressions/block-expr.html |
147 | 199 | [_Expression_]: expressions.html |
| 200 | +[_FunctionFront_]: items/functions.html |
| 201 | +[_FunctionParam_]: items/functions.html |
| 202 | +[_FunctionReturnType_]: items/functions.html |
148 | 203 | [_Generics_]: items/generics.html |
149 | | -[_Method_]: items/implementations.html |
150 | | -[_MethodType_]: items/implementations.html |
151 | 204 | [_OuterAttribute_]: attributes.html |
| 205 | +[_Pattern_]: patterns.html |
| 206 | +[_SelfParam_]: items/associated-items.html#methods |
152 | 207 | [_TypeParamBounds_]: trait-bounds.html |
153 | 208 | [_Type_]: types.html |
154 | 209 | [_WhereClause_]: items/generics.html#where-clauses |
|
0 commit comments