Skip to content

Commit 2a31152

Browse files
committed
Address assoc-item feedback.
1 parent 79c14b6 commit 2a31152

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

src/items/associated-items.md

+42-30
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[implementations]. They are called this because they are defined on an associate
55
type — the type in the implementation. They are a subset of the kinds of
66
items you can declare in a module. Specifically, there are [associated
7-
functions], [associated types], and [associated constants].
7+
functions] (including methods), [associated types], and [associated constants].
88

99
[associated functions]: #associated-functions-and-methods
1010
[associated types]: #associated-types
@@ -14,15 +14,20 @@ Associated items are useful when the associated item logically is related to the
1414
associating item. For example, the `is_some` method on `Option` is intrinsically
1515
related to Options, so should be associated.
1616

17-
Associated items are also the contract that traits have.
17+
Every associated item kind comes in two varieties: definitions that contain the
18+
actual implementation and declarations that declare specifications for
19+
definitions.
20+
21+
It is the declarations that make up the contract of traits and what it available
22+
on generic types.
1823

1924
## Associated functions and methods
2025

2126
*Associated functions* are [functions] associated with a type.
2227

23-
An *associated function declaration* is written as `fn`, then an [identifier]
24-
then optional generics, then `(` then a parameter list, then `)`, then
25-
optionally `->` and a type, then an optional where clause, then finally a `;`.
28+
An *associated function declaration* declares a specification for an associated
29+
function definition. It is written as a function item, except the
30+
function block is replaced with a `;`.
2631

2732
The identifier if the name of the function. The generics declares types for
2833
usage in the rest of the function declaration. The generics, parameter list,
@@ -38,7 +43,7 @@ a value of the type the associated function is associated with.
3843

3944
```rust
4045
struct Struct {
41-
field: i32;
46+
field: i32
4247
}
4348

4449
impl Struct {
@@ -50,9 +55,9 @@ impl Struct {
5055
}
5156
```
5257

53-
When the associated function is declared on a trait, the function can be called
54-
on the trait. When this happens, it is substituted for
55-
`<_ as Trait>::function_name`.
58+
When the associated function is declared on a trait, the function can also be
59+
called on the trait. When this happens, it is substituted for
60+
`<_ as Trait>::function_name`.
5661

5762
```rust
5863
trait Num {
@@ -69,12 +74,13 @@ Associated functions whose first parameter is named `self` are called *methods*
6974
and may be invoked using the [method call operator], for example, `x.foo()`, as
7075
well as the usual function call notation.
7176

72-
When the first parameter is named `self`, the following shorthands may be used:
77+
When the first parameter is named `self`, the following shorthands may be used.
7378

7479
* `self` -> `self: Self`
75-
* `&self` -> `self: &Self`
76-
* `&mut self` -> `&mut Self`
77-
* `Box<self>` -> `self: Box<Self>`
80+
* `&'lifetime self` -> `self: &'lifetime Self`
81+
* `&'lifetime mut self` -> `&'lifetime mut Self`
82+
83+
> Note: Lifetimes can be and usually are elided with this shorthand.
7884
7985
Consider the following trait:
8086

@@ -88,7 +94,8 @@ trait Shape {
8894
```
8995

9096
This defines a trait with two methods. All values that have [implementations]
91-
of this trait in scope can have their `draw` and `bounding_box` methods called.
97+
of this trait while the trait is in scope can have their `draw` and
98+
`bounding_box` methods called.
9299

93100
```rust
94101
# type Surface = i32;
@@ -104,11 +111,11 @@ struct Circle {
104111

105112
impl Shape for Circle {
106113
// ...
107-
# fn draw(&self, Surface) -> {}
108-
# fn bounding_box(&self) -> BoundingBox { 0i32; }
114+
# fn draw(&self, _: Surface) {}
115+
# fn bounding_box(&self) -> BoundingBox { 0i32 }
109116
}
110117

111-
# impl Box {
118+
# impl Circle {
112119
# fn new() -> Circle { Circle{} }
113120
}
114121

@@ -122,17 +129,20 @@ let bounding_box = circle_shape.bounding_box();
122129
types cannot be defined in [inherent implementations] nor can they be given a
123130
default implementation in traits.
124131

125-
An *associated type declaration* is written as `type`, then an [identifier], and
132+
An *associated type declaration* declares a specification for associated type
133+
definitions. It is written as `type`, then an [identifier], and
126134
finally an optional trait bounds.
127135

128136
The identifier is the name of the declared type alias. The optional trait bounds
129137
must be fulfilled by the implementations of the type alias.
130138

131-
An *associated type definition* is written as `type`, then an [identifier], then
132-
an `=`, and finally a [type].
139+
An *associated type definition* defines a type alias on another type. It is
140+
written as `type`, then an [identifier], then an `=`, and finally a [type].
133141

134-
If an item `Item` has an associated type `Assoc`, then `Item::Assoc` is a type
135-
that is an alias of the type specified in the associated type definition.
142+
If an item `Item` has an associated type `Assoc` from a trait `Trait`, then
143+
`<Item as Trait>::Assoc` is a type that is an alias of the type specified in the
144+
associated type definition. Furthermore, `Item::Assoc` can be used in type
145+
parameters.
136146

137147
```rust
138148
trait AssociatedType {
@@ -156,14 +166,14 @@ impl OtherStruct {
156166
}
157167

158168
fn main() {
159-
// Usage of the associated type to refer to OtherStruct as Struct::Assoc
160-
let _other_struct: OtherStruct = Struct::Assoc::new();
169+
// Usage of the associated type to refer to OtherStruct as <Struct as AssociatedType>::Assoc
170+
let _other_struct: OtherStruct = <Struct as AssociatedType>::Assoc::new();
161171
}
162172
```
163173

164174
### Associated Types Container Example
165175

166-
Consider the following example of a `Container` trait. Notice how the type is
176+
Consider the following example of a `Container` trait. Notice that the type is
167177
available for use in the method signatures:
168178

169179
```rust
@@ -195,14 +205,15 @@ impl<T> Container for Vec<T> {
195205

196206
*Associated constants* are [constants] associated with a type.
197207

198-
An *associated constant declaration* is written as `const`, then an identifier,
208+
An *associated constant declaration* declares a specificatoin for associated
209+
constant definitions. It is written as `const`, then an identifier,
199210
then `:`, then a type, finished by a `;`.
200211

201212
The identifier is the name of the constant used in the path. The type is the
202213
type that the definition has to implement.
203214

204-
An *associated constant definition* is written as a declaraction, but between
205-
the type and the `;`, there is an `=` followed by a [constant expression].
215+
An *associated constant definition* defines a constant associated with another
216+
type. It is written the same as a [constant item].
206217

207218
### Associated Constants Examples
208219

@@ -247,14 +258,15 @@ fn main() {
247258
```
248259

249260
[trait]: items/traits.html
261+
[traits]: items/traits.html
250262
[type aliases]: items/type-aliases.html
251263
[inherent implementations]: items/implementations.html#inherent-implementations
252264
[identifier]: identifiers.html
253265
[trait object]: types.html#trait-objects
254266
[implementations]: items/implementations.html
255267
[type]: types.html
256-
[constants]: items/constants.html
257-
[constant expression]: expressions.html#constant-expressions
268+
[constants]: items/constant-items.html
269+
[constant item]: items/constant-items.html
258270
[functions]: items/functions.html
259271
[method call operator]: expressions/method-call-expr.html
260272
[block]: expressions/block-expr.html

0 commit comments

Comments
 (0)