@@ -50,25 +50,50 @@ bracketed set of associable items.
50
50
The nominal type is called the _ implementing type_ and the associable items are
51
51
the _ associated items_ to the implementing type.
52
52
53
- Inherent implementations associate the contained items to the implementing type.
54
- The associated item has a path of a path to the implementing type followed by
55
- the associate item's path component. Inherent implementations cannot contain
56
- associated type aliases.
53
+ Inherent implementations associate the contained items to the
54
+ implementing type. Inherent implementations can contain [ associated
55
+ functions] (including methods) and [ associated constants] . They cannot
56
+ contain associated type aliases.
57
+
58
+ The [ path] to an associated item is any path to the implementing type,
59
+ followed by the associated item's identifier as the final path
60
+ component.
57
61
58
62
A type can also have multiple inherent implementations. An implementing type
59
63
must be defined within the same crate as the original type definition.
60
64
61
- ``` rust
62
- struct Point {x : i32 , y : i32 }
65
+ ``` rust
66
+ pub mod color {
67
+ pub struct Color (pub u8 , pub u8 , pub u8 );
68
+
69
+ impl Color {
70
+ pub const WHITE : Color = Color (255 , 255 , 255 );
71
+ }
72
+ }
63
73
64
- impl Point {
65
- fn log (& self ) {
66
- println! (" Point is at ({}, {})" , self . x, self . y);
74
+ mod values {
75
+ use super :: color :: Color ;
76
+ impl Color {
77
+ pub fn red () -> Color {
78
+ Color (255 , 0 , 0 )
79
+ }
67
80
}
68
81
}
69
82
70
- let my_point = Point {x : 10 , y : 11 };
71
- my_point . log ();
83
+ pub use self :: color :: Color ;
84
+ fn main () {
85
+ // Actual path to the implementing type and impl in the same module.
86
+ color :: Color :: WHITE ;
87
+
88
+ // Impl blocks in different modules are still accessed through a path to the type.
89
+ color :: Color :: red ();
90
+
91
+ // Re-exported paths to the implementing type also work.
92
+ Color :: red ();
93
+
94
+ // Does not work, because use in `values` is not pub.
95
+ // values::Color::red();
96
+ }
72
97
```
73
98
74
99
## Trait Implementations
@@ -191,9 +216,12 @@ attributes].
191
216
[ _Visibility_ ] : visibility-and-privacy.html
192
217
[ _WhereClause_ ] : items/generics.html#where-clauses
193
218
[ trait ] : items/traits.html
219
+ [ associated functions ] : items/associated-items.html#associated-functions-and-methods
220
+ [ associated constants ] : items/associated-items.html#associated-constants
194
221
[ attributes ] : attributes.html
195
222
[ `cfg` ] : conditional-compilation.html
196
223
[ `deprecated` ] : attributes.html#deprecation
197
224
[ `doc` ] : attributes.html#documentation
225
+ [ path ] : paths.html
198
226
[ the lint check attributes ] : attributes.html#lint-check-attributes
199
227
[ Unsafe traits ] : items/traits.html#unsafe-traits
0 commit comments