1
1
# Implementations
2
2
3
- An _ implementation_ is an item that associates items with an * implementing type* .
3
+ An _ implementation_ is an item that associates items with an _ implementing type_ .
4
+ Implementations are defined with the keyword ` impl ` and contain functions
5
+ that belong to an instance of the type that is being implemented or to the
6
+ type statically.
4
7
5
- There are two types of implementations: inherent implementations and [ trait]
6
- implementations.
8
+ There are two types of implementations:
7
9
8
- Implementations are defined with the keyword ` impl ` .
10
+ - inherent implementations
11
+ - [ trait] implementations
9
12
10
13
## Inherent Implementations
11
14
12
15
An inherent implementation is defined as the sequence of the ` impl ` keyword,
13
16
generic type declarations, a path to a nominal type, a where clause, and a
14
17
bracketed set of associable items.
15
18
16
- The nominal type is called the * implementing type* and the associable items are
17
- the * associated items* to the implementing type.
18
-
19
- Inherent implementations associate the associated items to the implementing
20
- type.
19
+ The nominal type is called the _ implementing type_ and the associable items are
20
+ the _ associated items_ to the implementing type.
21
21
22
+ Inherent implementations associate the contained items to the implementing type.
22
23
The associated item has a path of a path to the implementing type followed by
23
- the associate item's path component.
24
-
25
- Inherent implementations cannot contain associated type aliases.
24
+ the associate item's path component. Inherent implementations cannot contain
25
+ associated type aliases.
26
26
27
- A type can have multiple inherent implementations.
28
-
29
- The implementing type must be defined within the same crate.
27
+ A type can also have multiple inherent implementations. Although the
28
+ implementing type must be defined within the same crate.
30
29
31
30
``` rust
32
31
struct Point {x : i32 , y : i32 }
@@ -43,14 +42,13 @@ my_point.log();
43
42
44
43
## Trait Implementations
45
44
46
- A * trait implementation * is defined like an inherent implementation except that
45
+ A _ trait implementation _ is defined like an inherent implementation except that
47
46
the optional generic type declarations is followed by a [ trait] followed
48
- by the keyword ` for ` . <!-- To understand this, you have to back-reference to
49
- the previous section. :( -->
50
-
51
- The trait is known as the * implemented trait* .
47
+ by the keyword ` for ` . Followed by a path to a nominal type.
48
+ <!-- To understand this, you have to back-reference to the previous section. :( -->
52
49
53
- The implementing type implements the implemented trait.
50
+ The trait is known as the _ implemented trait_ . The implementing type
51
+ implements the implemented trait.
54
52
55
53
A trait implementation must define all non-default associated items declared
56
54
by the implemented trait, may redefine default associated items defined by the
@@ -105,18 +103,18 @@ be instantiable with the same set of types for the input type parameters. -->
105
103
The ` Orphan Check ` states that every trait implementation must meet either of
106
104
the following conditions:
107
105
108
- 1 . The trait being implemented is defined in the same crate.
106
+ 1 . The trait being implemented is defined in the same crate.
109
107
110
- 2 . At least one of either ` Self ` or a generic type parameter of the trait must
111
- meet the following grammar, where ` C ` is a nominal type defined
112
- within the containing crate:
108
+ 2 . At least one of either ` Self ` or a generic type parameter of the trait must
109
+ meet the following grammar, where ` C ` is a nominal type defined
110
+ within the containing crate:
113
111
114
- ``` ignore
115
- T = C
116
- | &C
117
- | &mut C
118
- | Box<C>
119
- ```
112
+ ``` ignore
113
+ T = C
114
+ | &C
115
+ | &mut C
116
+ | Box<C>
117
+ ```
120
118
121
119
## Generic Implementations
122
120
0 commit comments