|
1 |
| -# Type and Lifetime Parameters |
| 1 | +# Generic parameters |
2 | 2 |
|
3 | 3 | > **<sup>Syntax</sup>**\
|
4 | 4 | > _Generics_ :\
|
5 | 5 | > `<` _GenericParams_ `>`
|
6 | 6 | >
|
7 | 7 | > _GenericParams_ :\
|
8 | 8 | > _LifetimeParams_\
|
9 |
| -> | ( _LifetimeParam_ `,` )<sup>\*</sup> _TypeParams_ |
| 9 | +> | ( _LifetimeParam_ `,` )<sup>\*</sup> _TypeParams_\ |
| 10 | +> | ( _LifetimeParam_ `,` )<sup>\*</sup> ( _TypeParam_ `,` )<sup>\*</sup> _ConstParams_ |
10 | 11 | >
|
11 | 12 | > _LifetimeParams_ :\
|
12 | 13 | > ( _LifetimeParam_ `,` )<sup>\*</sup> _LifetimeParam_<sup>?</sup>
|
|
18 | 19 | > ( _TypeParam_ `,` )<sup>\*</sup> _TypeParam_<sup>?</sup>
|
19 | 20 | >
|
20 | 21 | > _TypeParam_ :\
|
21 |
| -> [_OuterAttribute_]<sup>?</sup> [IDENTIFIER] ( `:` [_TypeParamBounds_]<sup>?</sup> )<sup>?</sup> ( `=` [_Type_] )<sup>?</sup> |
| 22 | +> [_OuterAttribute_]<sup>?</sup> [IDENTIFIER]( `:` [_TypeParamBounds_]<sup>?</sup> )<sup>?</sup> ( `=` [_Type_] )<sup>?</sup> |
| 23 | +> |
| 24 | +> _ConstParams_:\ |
| 25 | +> ( _ConstParam_ `,` )<sup>\*</sup> _ConstParam_<sup>?</sup> |
| 26 | +> |
| 27 | +> _ConstParam_:\ |
| 28 | +> [_OuterAttribute_]<sup>?</sup> `const` [IDENTIFIER] `:` [_Type_] |
22 | 29 |
|
23 | 30 | Functions, type aliases, structs, enumerations, unions, traits, and
|
24 |
| -implementations may be *parameterized* by types and lifetimes. These parameters |
25 |
| -are listed in angle <span class="parenthetical">brackets (`<...>`)</span>, |
| 31 | +implementations may be *parameterized* by types, constants and lifetimes. These |
| 32 | +parameters are listed in angle <span class="parenthetical">brackets (`<...>`)</span>, |
26 | 33 | usually immediately after the name of the item and before its definition. For
|
27 | 34 | implementations, which don't have a name, they come directly after `impl`.
|
28 |
| -Lifetime parameters must be declared before type parameters. Some examples of |
29 |
| -items with type and lifetime parameters: |
| 35 | +The order of generic parameters is restricted to lifetime parameters, then type parameters and then const parameters. |
| 36 | + |
| 37 | +The only allowed types of const parameters are `u8`, `u16`, `u32`, `u64`, `u128`, `usize` |
| 38 | +`i8`, `i16`, `i32`, `i64`, `i128`, `isize`, `char` and `bool`. |
| 39 | +Const parameters may only be be used as standalone arguments inside |
| 40 | +of [types] and [repeat expressions]. |
| 41 | +They can be used freely outside of [const contexts]. |
| 42 | + |
| 43 | +Some examples of items with type, const and lifetime parameters: |
30 | 44 |
|
31 | 45 | ```rust
|
32 | 46 | fn foo<'a, T>() {}
|
33 | 47 | trait A<U> {}
|
34 | 48 | struct Ref<'a, T> where T: 'a { r: &'a T }
|
| 49 | +struct InnerArray<T, const N: usize>([T; N]); |
35 | 50 | ```
|
36 | 51 |
|
37 | 52 | [References], [raw pointers], [arrays], [slices][arrays], [tuples], and
|
@@ -65,7 +80,7 @@ Bounds that don't use the item's parameters or higher-ranked lifetimes are
|
65 | 80 | checked when the item is defined. It is an error for such a bound to be false.
|
66 | 81 |
|
67 | 82 | [`Copy`], [`Clone`], and [`Sized`] bounds are also checked for certain generic
|
68 |
| -types when defining the item. It is an error to have `Copy` or `Clone`as a |
| 83 | +types when defining the item. It is an error to have `Copy` or `Clone` as a |
69 | 84 | bound on a mutable reference, [trait object] or [slice][arrays] or `Sized` as a
|
70 | 85 | bound on a trait object or slice.
|
71 | 86 |
|
@@ -112,12 +127,15 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
|
112 | 127 | [_TypeParamBounds_]: ../trait-bounds.md
|
113 | 128 |
|
114 | 129 | [arrays]: ../types/array.md
|
| 130 | +[const contexts]: ../const_eval.md#const-context |
115 | 131 | [function pointers]: ../types/function-pointer.md
|
116 |
| -[references]: ../types/pointer.md#shared-references- |
117 | 132 | [raw pointers]: ../types/pointer.md#raw-pointers-const-and-mut
|
| 133 | +[references]: ../types/pointer.md#shared-references |
| 134 | +[repeat expressions]: ../expressions/array-expr.md |
118 | 135 | [`Clone`]: ../special-types-and-traits.md#clone
|
119 | 136 | [`Copy`]: ../special-types-and-traits.md#copy
|
120 | 137 | [`Sized`]: ../special-types-and-traits.md#sized
|
121 | 138 | [tuples]: ../types/tuple.md
|
122 | 139 | [trait object]: ../types/trait-object.md
|
| 140 | +[types]: ../types.md |
123 | 141 | [attributes]: ../attributes.md
|
0 commit comments