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
+ Some examples of items with type, const, and lifetime parameters:
30
38
31
39
``` rust
32
40
fn foo <'a , T >() {}
33
41
trait A <U > {}
34
42
struct Ref <'a , T > where T : 'a { r : & 'a T }
43
+ struct InnerArray <T , const N : usize >([T ; N ]);
44
+ ```
45
+
46
+ The only allowed types of const parameters are ` u8 ` , ` u16 ` , ` u32 ` , ` u64 ` , ` u128 ` , ` usize `
47
+ ` i8 ` , ` i16 ` , ` i32 ` , ` i64 ` , ` i128 ` , ` isize ` , ` char ` and ` bool ` .
48
+
49
+ Const parameters may only be be used as standalone arguments inside
50
+ of [ types] and [ repeat expressions] but may be freely used elsewhere:
51
+
52
+ ``` rust,compile_fail
53
+ // ok: standalone argument
54
+ fn foo<const N: usize>() -> [u8; N] { todo!() }
55
+
56
+ // ERROR: generic const operation
57
+ fn bar<const N: usize>() -> [u8; N + 1] { todo!() }
58
+ ```
59
+
60
+ Unlike type and lifetime parameters, const parameters of types can be used without
61
+ being mentioned inside of a parameterized type:
62
+
63
+ ``` rust,compile_fail
64
+ // ok
65
+ struct Foo<const N: usize>;
66
+ enum Bar<const M: usize> { A, B }
67
+
68
+ // ERROR: unused parameter
69
+ struct Baz<T>;
70
+ struct Biz<'a>;
35
71
```
36
72
37
73
[ References] , [ raw pointers] , [ arrays] , [ slices] [ arrays ] , [ tuples] , and
@@ -55,7 +91,7 @@ referred to with path syntax.
55
91
>   ;  ; _ ForLifetimes_ <sup >?</sup > [ _ Type_ ] ` : ` [ _ TypeParamBounds_ ] <sup >?</sup >
56
92
>
57
93
> _ ForLifetimes_ :\
58
- >   ;  ; ` for ` ` < ` [ _ LifetimeParams_ ] ( #type-and-lifetime -parameters ) ` > `
94
+ >   ;  ; ` for ` ` < ` [ _ LifetimeParams_ ] ( #generic -parameters ) ` > `
59
95
60
96
* Where clauses* provide another way to specify bounds on type and lifetime
61
97
parameters as well as a way to specify bounds on types that aren't type
@@ -65,7 +101,7 @@ Bounds that don't use the item's parameters or higher-ranked lifetimes are
65
101
checked when the item is defined. It is an error for such a bound to be false.
66
102
67
103
[ ` 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
104
+ types when defining the item. It is an error to have ` Copy ` or ` Clone ` as a
69
105
bound on a mutable reference, [ trait object] or [ slice] [ arrays ] or ` Sized ` as a
70
106
bound on a trait object or slice.
71
107
@@ -112,12 +148,15 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
112
148
[ _TypeParamBounds_ ] : ../trait-bounds.md
113
149
114
150
[ arrays ] : ../types/array.md
151
+ [ const contexts ] : ../const_eval.md#const-context
115
152
[ function pointers ] : ../types/function-pointer.md
116
- [ references ] : ../types/pointer.md#shared-references-
117
153
[ raw pointers ] : ../types/pointer.md#raw-pointers-const-and-mut
154
+ [ references ] : ../types/pointer.md#shared-references-
155
+ [ repeat expressions ] : ../expressions/array-expr.md
118
156
[ `Clone` ] : ../special-types-and-traits.md#clone
119
157
[ `Copy` ] : ../special-types-and-traits.md#copy
120
158
[ `Sized` ] : ../special-types-and-traits.md#sized
121
159
[ tuples ] : ../types/tuple.md
122
160
[ trait object ] : ../types/trait-object.md
161
+ [ types ] : ../types.md
123
162
[ attributes ] : ../attributes.md
0 commit comments