Skip to content

Commit bf9ba61

Browse files
committed
Address associated items feedback round 2
1 parent 2a31152 commit bf9ba61

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/items/associated-items.md

+32-24
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ associating item. For example, the `is_some` method on `Option` is intrinsically
1515
related to Options, so should be associated.
1616

1717
Every associated item kind comes in two varieties: definitions that contain the
18-
actual implementation and declarations that declare specifications for
18+
actual implementation and declarations that declare signatures for
1919
definitions.
2020

2121
It is the declarations that make up the contract of traits and what it available
@@ -25,20 +25,18 @@ on generic types.
2525

2626
*Associated functions* are [functions] associated with a type.
2727

28-
An *associated function declaration* declares a specification for an associated
28+
An *associated function declaration* declares a signature for an associated
2929
function definition. It is written as a function item, except the
30-
function block is replaced with a `;`.
30+
function body is replaced with a `;`.
3131

32-
The identifier if the name of the function. The generics declares types for
33-
usage in the rest of the function declaration. The generics, parameter list,
34-
return type, and where clause must be the same in the associated function
35-
definition.
32+
The identifier if the name of the function. The generics, parameter list,
33+
return type, and where clause of the associated function must be the same as the
34+
associated function declarations's.
3635

37-
An *associated function definiton* is written as an associated function
38-
declaration, but instead of a `;`, there is a [block] that evaluates to the
39-
return type.
36+
An *associated function definiton* defines a function associated with another
37+
type. It is written the same as a [function item].
4038

41-
An example of a common associated function is the `new` function that returns
39+
An example of a common associated function is a `new` function that returns
4240
a value of the type the associated function is associated with.
4341

4442
```rust
@@ -53,21 +51,30 @@ impl Struct {
5351
}
5452
}
5553
}
54+
55+
fn main () {
56+
let _struct = Struct::new();
57+
}
5658
```
5759

5860
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`.
61+
called with a [path] that is a path to the trait appended by the name of the
62+
trait. When this happens, it is substituted for `<_ as Trait>::function_name`.
6163

6264
```rust
6365
trait Num {
6466
fn from_i32(n: i32) -> Self;
6567
}
68+
6669
impl Num for f64 {
6770
fn from_i32(n: i32) -> f64 { n as f64 }
6871
}
69-
let x: f64 = Num::from_i32(42);
70-
let x: f64 = f64::from_i32(42);
72+
73+
// These 4 are all equivalent in this case.
74+
let _: f64 = Num::from_i32(42);
75+
let _: f64 = <_ as Num>::from_i32(42);
76+
let _: f64 = <f64 as Num>::from_i32(42);
77+
let _: f64 = f64::from_i32(42);
7178
```
7279

7380
Associated functions whose first parameter is named `self` are called *methods*
@@ -78,7 +85,7 @@ When the first parameter is named `self`, the following shorthands may be used.
7885

7986
* `self` -> `self: Self`
8087
* `&'lifetime self` -> `self: &'lifetime Self`
81-
* `&'lifetime mut self` -> `&'lifetime mut Self`
88+
* `&'lifetime mut self` -> `self: &'lifetime mut Self`
8289

8390
> Note: Lifetimes can be and usually are elided with this shorthand.
8491
@@ -129,20 +136,20 @@ let bounding_box = circle_shape.bounding_box();
129136
types cannot be defined in [inherent implementations] nor can they be given a
130137
default implementation in traits.
131138

132-
An *associated type declaration* declares a specification for associated type
139+
An *associated type declaration* declares a signature for associated type
133140
definitions. It is written as `type`, then an [identifier], and
134-
finally an optional trait bounds.
141+
finally an optional list of trait bounds.
135142

136143
The identifier is the name of the declared type alias. The optional trait bounds
137144
must be fulfilled by the implementations of the type alias.
138145

139146
An *associated type definition* defines a type alias on another type. It is
140147
written as `type`, then an [identifier], then an `=`, and finally a [type].
141148

142-
If an item `Item` has an associated type `Assoc` from a trait `Trait`, then
149+
If a type `Item` has an associated type `Assoc` from a trait `Trait`, then
143150
`<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.
151+
associated type definition. Furthermore, if `Item` is a type parameter, then
152+
`Item::Assoc` can be used in type parameters.
146153

147154
```rust
148155
trait AssociatedType {
@@ -205,14 +212,14 @@ impl<T> Container for Vec<T> {
205212

206213
*Associated constants* are [constants] associated with a type.
207214

208-
An *associated constant declaration* declares a specificatoin for associated
215+
An *associated constant declaration* declares a signature for associated
209216
constant definitions. It is written as `const`, then an identifier,
210217
then `:`, then a type, finished by a `;`.
211218

212219
The identifier is the name of the constant used in the path. The type is the
213220
type that the definition has to implement.
214221

215-
An *associated constant definition* defines a constant associated with another
222+
An *associated constant definition* defines a constant associated with a
216223
type. It is written the same as a [constant item].
217224

218225
### Associated Constants Examples
@@ -269,4 +276,5 @@ fn main() {
269276
[constant item]: items/constant-items.html
270277
[functions]: items/functions.html
271278
[method call operator]: expressions/method-call-expr.html
272-
[block]: expressions/block-expr.html
279+
[block]: expressions/block-expr.html
280+
[path]: paths.html

0 commit comments

Comments
 (0)