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.
26
-
27
- A type can have multiple inherent implementations.
24
+ the associate item's path component. Inherent implementations cannot contain
25
+ associated type aliases.
28
26
29
- The implementing type must be defined within the same crate.
27
+ A type can also have multiple inherent implementations. An implementing type
28
+ must be defined within the same crate as the original type definition.
30
29
31
30
``` rust
32
31
struct Point {x : i32 , y : i32 }
@@ -43,14 +42,14 @@ 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. :( -->
47
+ by the keyword ` for ` . Followed by a path to a nominal type.
50
48
51
- The trait is known as the * implemented trait * .
49
+ <!-- To understand this, you have to back-reference to the previous section. :( -->
52
50
53
- The implementing type implements the implemented trait.
51
+ The trait is known as the _ implemented trait_ . The implementing type
52
+ implements the implemented trait.
54
53
55
54
A trait implementation must define all non-default associated items declared
56
55
by the implemented trait, may redefine default associated items defined by the
@@ -105,18 +104,18 @@ be instantiable with the same set of types for the input type parameters. -->
105
104
The ` Orphan Check ` states that every trait implementation must meet either of
106
105
the following conditions:
107
106
108
- 1 . The trait being implemented is defined in the same crate.
107
+ 1 . The trait being implemented is defined in the same crate.
109
108
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:
109
+ 2 . At least one of either ` Self ` or a generic type parameter of the trait must
110
+ meet the following grammar, where ` C ` is a nominal type defined
111
+ within the containing crate:
113
112
114
- ``` ignore
115
- T = C
116
- | &C
117
- | &mut C
118
- | Box<C>
119
- ```
113
+ ``` ignore
114
+ T = C
115
+ | &C
116
+ | &mut C
117
+ | Box<C>
118
+ ```
120
119
121
120
## Generic Implementations
122
121
0 commit comments