Skip to content

Commit f6d6871

Browse files
committed
Move type parameters and trait bounds to their own page
1 parent 86b7c82 commit f6d6871

9 files changed

+59
-58
lines changed

src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- [Traits](items/traits.md)
3535
- [Implementations](items/implementations.md)
3636
- [External blocks](items/external-blocks.md)
37+
- [Type and lifetime parameters](items/generics.md)
3738
- [Associated Items](items/associated-items.md)
3839
- [Visibility and Privacy](visibility-and-privacy.md)
3940
- [Attributes](attributes.md)
@@ -64,6 +65,7 @@
6465
- [Dynamically Sized Types](dynamically-sized-types.md)
6566
- [Type layout](type-layout.md)
6667
- [Interior mutability](interior-mutability.md)
68+
- [Trait and lifetime bounds](trait-bounds.md)
6769
- [Subtyping](subtyping.md)
6870
- [Type coercions](type-coercions.md)
6971
- [Destructors](destructors.md)

src/items.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,3 @@ as if the item was declared outside the scope — it is still a static item
3636
qualified by the name of the enclosing item, or is private to the enclosing
3737
item (in the case of functions). The grammar specifies the exact locations in
3838
which sub-item declarations may appear.
39-
40-
## Type Parameters
41-
42-
Functions, type aliases, structs, enumerations, unions, traits and
43-
implementations may be *parameterized* by type. Type parameters are given as a
44-
comma-separated list of identifiers enclosed in angle brackets (`<...>`), after
45-
the name of the item (except for implementations, where they come directly
46-
after `impl`) and before its definition.
47-
48-
The type parameters of an item are considered "part of the name", not part of
49-
the type of the item. A referencing [path] must (in principle) provide type
50-
arguments as a list of comma-separated types enclosed within angle brackets, in
51-
order to refer to the type-parameterized item. In practice, the type-inference
52-
system can usually infer such argument types from context. There are no general
53-
type-parametric types, only type-parametric items. That is, Rust has no notion
54-
of type abstraction: there are no higher-ranked (or "forall") types abstracted
55-
over other types, though higher-ranked types do exist for lifetimes.
56-
57-
[path]: paths.html

src/items/enumerations.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ enum ZeroVariants {}
132132
```
133133

134134
[IDENTIFIER]: identifiers.html
135-
[_Generics_]: items.html#type-parameters
136-
[_WhereClause_]: items.html#type-parameters
135+
[_Generics_]: items/generics.html
136+
[_WhereClause_]: iitems/generics.html#where-clauses
137137
[_Expression_]: expressions.html
138138
[_TupleFields_]: items/structs.html
139139
[_StructFields_]: items/structs.html

src/items/generics.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Type Parameters
2+
3+
Functions, type aliases, structs, enumerations, unions, traits and
4+
implementations may be *parameterized* by type. Type parameters are given as a
5+
comma-separated list of identifiers enclosed in angle brackets (`<...>`), after
6+
the name of the item (except for implementations, where they come directly
7+
after `impl`) and before its definition.
8+
9+
The type parameters of an item are considered "part of the name", not part of
10+
the type of the item. A referencing [path] must (in principle) provide type
11+
arguments as a list of comma-separated types enclosed within angle brackets, in
12+
order to refer to the type-parameterized item. In practice, the type-inference
13+
system can usually infer such argument types from context. There are no general
14+
type-parametric types, only type-parametric items. That is, Rust has no notion
15+
of type abstraction: there are no higher-ranked (or "forall") types abstracted
16+
over other types, though higher-ranked types do exist for lifetimes.
17+
18+
[path]: paths.html

src/items/structs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ particular layout using the [`repr` attribute].
8282

8383
[_OuterAttribute_]: attributes.html
8484
[IDENTIFIER]: identifiers.html
85-
[_Generics_]: items.html#type-parameters
86-
[_WhereClause_]: items.html#type-parameters
85+
[_Generics_]: items/generics.html
86+
[_WhereClause_]: items/generics.html#where-clauses
8787
[_Visibility_]: visibility-and-privacy.html
8888
[_Type_]: types.html

src/items/traits.md

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,7 @@ Traits are implemented for specific types through separate [implementations].
1616

1717
## Trait bounds
1818

19-
Generic functions may use traits as _bounds_ on their type parameters. This
20-
will have three effects:
21-
22-
- Only types that have the trait may instantiate the parameter.
23-
- Within the generic function, the methods of the trait can be called on values
24-
that have the parameter's type. Associated types can be used in the
25-
function's signature, and associated constants can be used in expressions
26-
within the function body.
27-
- Generic functions and types with the same or weaker bounds can use the
28-
generic type in the function body or signature.
29-
30-
For example:
31-
32-
```rust
33-
# type Surface = i32;
34-
# trait Shape { fn draw(&self, Surface); }
35-
struct Figure<S: Shape>(S, S);
36-
fn draw_twice<T: Shape>(surface: Surface, sh: T) {
37-
sh.draw(surface);
38-
sh.draw(surface);
39-
}
40-
fn draw_figure<U: Shape>(surface: Surface, Figure(sh1, sh2): Figure<U>) {
41-
sh1.draw(surface);
42-
draw_twice(surface, sh2); // Can call this since U: Shape
43-
}
44-
```
19+
Generic items may use traits as [bounds] on their type parameters.
4520

4621
## Generic Traits
4722

@@ -136,12 +111,9 @@ let mycircle = Box::new(mycircle) as Box<Circle>;
136111
let nonsense = mycircle.radius() * mycircle.area();
137112
```
138113

139-
[`Send`]: ../std/marker/trait.Send.html
140-
[`Send`]: ../std/marker/trait.Sync.html
141-
[`UnwindSafe`]: ../std/panic/trait.UnwindSafe.html
142-
[`RefUnwindSafe`]: ../std/panic/trait.RefUnwindSafe.html
114+
[bounds]: trait-bounds.html
143115
[trait object]: types.html#trait-objects
144116
[explicit]: expressions/operator-expr.html#type-cast-expressions
145117
[methods called]: expressions/method-call-expr.html
146118
[RFC 255]: https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md
147-
[associated items]: items/associated-items.html
119+
[associated items]: items/associated-items.html

src/items/type-aliases.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ let _: F = E::A; // OK
3030
```
3131

3232
[IDENTIFIER]: identifiers.html
33-
[_Generics_]: items.html#type-parameters
34-
[_WhereClause_]: items.html#type-parameters
33+
[_Generics_]: items/generics.html
34+
[_WhereClause_]: items/generics.html#where-clauses
3535
[_Type_]: types.html

src/items/unions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,6 @@ More detailed specification for unions, including unstable bits, can be found
146146
in [RFC 1897 "Unions v1.2"](https://github.com/rust-lang/rfcs/pull/1897).
147147

148148
[IDENTIFIER]: identifiers.html
149-
[_Generics_]: items.html#type-parameters
150-
[_WhereClause_]: items.html#type-parameters
149+
[_Generics_]: items/generics.html
150+
[_WhereClause_]: items/generics.html#where-clauses
151151
[_StructFields_]: items/structs.html

src/trait-bounds.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Trait bounds
2+
3+
Generic functions may use traits as _bounds_ on their type parameters. This
4+
will have three effects:
5+
6+
- Only types that have the trait may instantiate the parameter.
7+
- Within the generic function, the methods of the trait can be called on values
8+
that have the parameter's type. Associated types can be used in the
9+
function's signature, and associated constants can be used in expressions
10+
within the function body.
11+
- Generic functions and types with the same or weaker bounds can use the
12+
generic type in the function body or signature.
13+
14+
For example:
15+
16+
```rust
17+
# type Surface = i32;
18+
# trait Shape { fn draw(&self, Surface); }
19+
struct Figure<S: Shape>(S, S);
20+
fn draw_twice<T: Shape>(surface: Surface, sh: T) {
21+
sh.draw(surface);
22+
sh.draw(surface);
23+
}
24+
fn draw_figure<U: Shape>(surface: Surface, Figure(sh1, sh2): Figure<U>) {
25+
sh1.draw(surface);
26+
draw_twice(surface, sh2); // Can call this since U: Shape
27+
}
28+
```

0 commit comments

Comments
 (0)