@@ -50,25 +50,50 @@ bracketed set of associable items.
5050The nominal type is called the _ implementing type_ and the associable items are
5151the _ associated items_ to the implementing type.
5252
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.
5761
5862A type can also have multiple inherent implementations. An implementing type
5963must be defined within the same crate as the original type definition.
6064
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+ }
6373
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+ }
6780 }
6881}
6982
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+ }
7297```
7398
7499## Trait Implementations
@@ -191,9 +216,12 @@ attributes].
191216[ _Visibility_ ] : visibility-and-privacy.html
192217[ _WhereClause_ ] : items/generics.html#where-clauses
193218[ trait ] : items/traits.html
219+ [ associated functions ] : items/associated-items.html#associated-functions-and-methods
220+ [ associated constants ] : items/associated-items.html#associated-constants
194221[ attributes ] : attributes.html
195222[ `cfg` ] : conditional-compilation.html
196223[ `deprecated` ] : attributes.html#deprecation
197224[ `doc` ] : attributes.html#documentation
225+ [ path ] : paths.html
198226[ the lint check attributes ] : attributes.html#lint-check-attributes
199227[ Unsafe traits ] : items/traits.html#unsafe-traits
0 commit comments