Skip to content

Commit 015fad3

Browse files
committed
Add an example and more explanation about associate item paths
1 parent 5e0c77f commit 015fad3

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/items/implementations.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,36 @@ The nominal type is called the _implementing type_ and the associable items are
5151
the _associated items_ to the implementing type.
5252

5353
Inherent implementations associate the contained items to the implementing type.
54-
The path of an associated item consists of a path to the implementing type followed by
54+
The path to an associated item is: any path to the implementing type followed by
5555
the associate item's path component. Inherent implementations cannot contain
5656
associated type aliases.
5757

58+
Note that the path to the module containing the inherent
59+
implementation does not allow access to the associate item, unless the
60+
implementing type is re-exported from the same location.
61+
62+
``` rust
63+
pub mod color {
64+
pub struct Color(pub u8, pub u8, pub u8);
65+
}
66+
67+
mod values {
68+
use super::color::Color;
69+
impl Color {
70+
pub fn red() -> Color {
71+
Color(255, 0, 0)
72+
}
73+
}
74+
}
75+
76+
pub use self::color::Color;
77+
fn main() {
78+
color::Color::red(); // actual path to the implementing type
79+
Color::red(); // rexported paths to the implementing type also work
80+
// bright_theme::Color::red(); // Does not work, because use in `theme` is not pub
81+
}
82+
```
83+
5884
A type can also have multiple inherent implementations. An implementing type
5985
must be defined within the same crate as the original type definition.
6086

0 commit comments

Comments
 (0)