Skip to content

Commit c3dd948

Browse files
authored
Merge pull request #493 from RadicalZephyr/clarifications
Clarifications
2 parents 404235e + 83cb560 commit c3dd948

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/items/generics.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
Functions, type aliases, structs, enumerations, unions, traits and
2424
implementations may be *parameterized* by types and lifetimes. These parameters
2525
are listed in angle <span class="parenthetical">brackets (`<...>`)</span>,
26-
usually immediately after and before its definition the name of the item. For
26+
usually immediately after the name of the item and before its definition. For
2727
implementations, which don't have a name, they come directly after `impl`.
2828
Lifetime parameters must be declared before type parameters. Some examples of
2929
items with type and lifetime parameters:
@@ -57,7 +57,7 @@ referred to with path syntax.
5757
> _ForLifetimes_ :\
5858
> &nbsp;&nbsp; `for` `<` [_LifetimeParams_](#type-and-lifetime-parameters) `>`
5959
60-
*Where clauses* provide an another way to specify bounds on type and lifetime
60+
*Where clauses* provide another way to specify bounds on type and lifetime
6161
parameters as well as a way to specify bounds on types that aren't type
6262
parameters.
6363

src/items/implementations.md

+39-11
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,50 @@ bracketed set of associable items.
5050
The nominal type is called the _implementing type_ and the associable items are
5151
the _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

5862
A type can also have multiple inherent implementations. An implementing type
5963
must 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

Comments
 (0)