|
1 | 1 | # Implementations |
2 | 2 |
|
| 3 | +> **<sup>Syntax</sup>**\ |
| 4 | +> _Implementation_ :\ |
| 5 | +> _InherentImpl_ | _TraitImpl_ |
| 6 | +> |
| 7 | +> _InherentImpl_ :\ |
| 8 | +> `impl` [_GenericsDecl_] [_Type_] [_WhereClause_]<sup>?</sup> `{`\ |
| 9 | +> [_InnerAttribute_]<sup>\*</sup>\ |
| 10 | +> _InherentImplItem_<sup>\*</sup>\ |
| 11 | +> `}` |
| 12 | +> |
| 13 | +> _InherentImplItem_ :\ |
| 14 | +> [_OuterAttribute_]<sup>\*</sup>\ |
| 15 | +> [_Visibility_]<sup>?</sup>\ |
| 16 | +> ( [_ConstantItem_] | _Method_ ) |
| 17 | +> |
| 18 | +> _TraitImpl_ :\ |
| 19 | +> `unsafe`<sup>?</sup> `impl` [_GenericsDecl_] `!`<sup>?</sup> |
| 20 | +> [_TypePath_] `for` [_Type_]\ |
| 21 | +> [_WhereClause_]<sup>?</sup>\ |
| 22 | +> `{`\ |
| 23 | +> [_InnerAttribute_]<sup>\*</sup>\ |
| 24 | +> _TraitImplItem_<sup>\*</sup>\ |
| 25 | +> `}` |
| 26 | +> |
| 27 | +> _ImplItem_ :\ |
| 28 | +> [_OuterAttribute_]<sup>\*</sup>\ |
| 29 | +> [_Visibility_]<sup>?</sup>\ |
| 30 | +> ( [_TypeAlias_] | [_ConstantItem_] | _Method_ ) |
| 31 | +> |
| 32 | +> _Method_:\ |
| 33 | +> _MethodType_ [_BlockWithInnerAttributes_] |
| 34 | +> |
| 35 | +> _MethodType_:\ |
| 36 | +> [_FunctionFront_] `fn` [IDENTIFIER] [_Generics_]<sup>?</sup>\ |
| 37 | +> `(` _MethodParameters_<sup>?</sup> `)`\ |
| 38 | +> [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup> |
| 39 | +> |
| 40 | +> _MethodParameters_:\ |
| 41 | +> (_SelfParam_ | [_FunctionParam_] ) (`,` [_FunctionParam_])<sup>\*</sup> `,`<sup>?</sup> |
| 42 | +> |
| 43 | +> _SelfParam_:\ |
| 44 | +> (`&` | `&` [_Lifetime_])<sup>?</sup> `mut`<sup>?</sup> `self`\ |
| 45 | +> | `mut`<sup>?</sup> `self` (`:` [_Type_])<sup>?</sup> |
| 46 | +
|
3 | 47 | An _implementation_ is an item that associates items with an _implementing type_. |
4 | 48 | Implementations are defined with the keyword `impl` and contain functions |
5 | 49 | that belong to an instance of the type that is being implemented or to the |
@@ -59,6 +103,9 @@ The path to the associated items is `<` followed by a path to the implementing |
59 | 103 | type followed by `as` followed by a path to the trait followed by `>` as a path |
60 | 104 | component followed by the associated item's path component. |
61 | 105 |
|
| 106 | +[Unsafe traits] require the trait implementation to begin with the `unsafe` |
| 107 | +keyword. |
| 108 | + |
62 | 109 | ```rust |
63 | 110 | # #[derive(Copy, Clone)] |
64 | 111 | # struct Point {x: f64, y: f64}; |
@@ -143,9 +190,26 @@ attributes must come before any associated items. That attributes that have |
143 | 190 | meaning here are [`cfg`], [`deprecated`], [`doc`], and [the lint check |
144 | 191 | attributes]. |
145 | 192 |
|
| 193 | +[IDENTIFIER]: identifiers.html |
| 194 | +[_BlockWithInnerAttributes_]: items/functions.html |
| 195 | +[_ConstantItem_]: items/constant-items.html |
| 196 | +[_Generics_]: items/generics.html |
| 197 | +[_FunctionFront_]: items/functions.html |
| 198 | +[_FunctionParam_]: items/functions.html |
| 199 | +[_FunctionReturnType_]: items/functions.html |
| 200 | +[_GenericsDecl_]: types.html#type-parameters |
| 201 | +[_InnerAttribute_]: attributes.html |
| 202 | +[_Lifetime_]: trait-bounds.html |
| 203 | +[_OuterAttribute_]: attributes.html |
| 204 | +[_Type_]: types.html |
| 205 | +[_TypeAlias_]: items/type-aliases.html |
| 206 | +[_TypePath_]: paths.html#paths-in-types |
| 207 | +[_Visibility_]: visibility-and-privacy.html |
| 208 | +[_WhereClause_]: items/generics.html#where-clauses |
146 | 209 | [trait]: items/traits.html |
147 | 210 | [attributes]: attributes.html |
148 | 211 | [`cfg`]: conditional-compilation.html |
149 | 212 | [`deprecated`]: attributes.html#deprecation |
150 | 213 | [`doc`]: attributes.html#documentation |
151 | 214 | [the lint check attributes]: attributes.html#lint-check-attributes |
| 215 | +[Unsafe traits]: items/traits.html#unsafe-traits |
0 commit comments