1
1
# Generic parameters
2
2
3
3
> ** <sup >Syntax</sup >** \
4
- > _ Generics_ :\
5
- >   ;  ; ` < ` _ GenericParams_ ` > `
6
- >
7
4
> _ GenericParams_ :\
8
- >   ;  ;   ;  ; _ LifetimeParams_ \
9
- >   ;  ; | ( _ LifetimeParam_ ` , ` )<sup >\* </sup > _ TypeParams_ \
10
- >   ;  ; | ( _ LifetimeParam_ ` , ` )<sup >\* </sup > ( _ TypeParam_ ` , ` )<sup >\* </sup > _ ConstParams_
5
+ >   ;  ;   ;  ; ` < ` ` > ` \
6
+ >   ;  ; | ` < ` (_ GenericParam_ ` , ` )<sup >\* </sup > _ GenericParam_ ` , ` <sup >?</sup > ` > `
11
7
>
12
- > _ LifetimeParams _ :\
13
- >   ;  ; ( _ LifetimeParam _ ` , ` ) <sup >\* </sup > _ LifetimeParam_ < sup >?</ sup >
8
+ > _ GenericParam _ :\
9
+ >   ;  ; [ _ OuterAttribute _ ] <sup >\* </sup > ( _ LifetimeParam_ | _ TypeParam _ | _ ConstParam _ )
14
10
>
15
11
> _ LifetimeParam_ :\
16
- >   ;  ; [ _ OuterAttribute_ ] <sup >?</sup > [ LIFETIME_OR_LABEL]   ; ( ` : ` [ _ LifetimeBounds_ ] )<sup >?</sup >
17
- >
18
- > _ TypeParams_ :\
19
- >   ;  ; ( _ TypeParam_ ` , ` )<sup >\* </sup > _ TypeParam_ <sup >?</sup >
12
+ >   ;  ; [ LIFETIME_OR_LABEL]   ; ( ` : ` [ _ LifetimeBounds_ ] )<sup >?</sup >
20
13
>
21
14
> _ TypeParam_ :\
22
- >   ;  ; [ _ OuterAttribute_ ] <sup >?</sup > [ IDENTIFIER] ( ` : ` [ _ TypeParamBounds_ ] <sup >?</sup > )<sup >?</sup > ( ` = ` [ _ Type_ ] )<sup >?</sup >
23
- >
24
- > _ ConstParams_ :\
25
- >   ;  ; ( _ ConstParam_ ` , ` )<sup >\* </sup > _ ConstParam_ <sup >?</sup >
15
+ >   ;  ; [ IDENTIFIER] ( ` : ` [ _ TypeParamBounds_ ] <sup >?</sup > )<sup >?</sup > ( ` = ` [ _ Type_ ] )<sup >?</sup >
26
16
>
27
17
> _ ConstParam_ :\
28
- >   ;  ; [ _ OuterAttribute _ ] < sup >?</ sup > ` const ` [ IDENTIFIER] ` : ` [ _ Type_ ]
18
+ >   ;  ; ` const ` [ IDENTIFIER] ` : ` [ _ Type_ ]
29
19
30
20
Functions, type aliases, structs, enumerations, unions, traits, and
31
21
implementations may be * parameterized* by types, constants, and lifetimes. These
@@ -91,12 +81,15 @@ referred to with path syntax.
91
81
>   ;  ; _ ForLifetimes_ <sup >?</sup > [ _ Type_ ] ` : ` [ _ TypeParamBounds_ ] <sup >?</sup >
92
82
>
93
83
> _ ForLifetimes_ :\
94
- >   ;  ; ` for ` ` < ` [ _ LifetimeParams _ ] ( #generic-parameters ) ` > `
84
+ >   ;  ; ` for ` [ _ GenericParams _ ] ( #generic-parameters )
95
85
96
86
* Where clauses* provide another way to specify bounds on type and lifetime
97
87
parameters as well as a way to specify bounds on types that aren't type
98
88
parameters.
99
89
90
+ The ` for ` keyword can be used to introduce [ higher-ranked lifetimes] . It only
91
+ allows [ _ LifetimeParam_ ] parameters.
92
+
100
93
Bounds that don't use the item's parameters or higher-ranked lifetimes are
101
94
checked when the item is defined. It is an error for such a bound to be false.
102
95
@@ -141,6 +134,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
141
134
[ IDENTIFIER ] : ../identifiers.md
142
135
[ LIFETIME_OR_LABEL ] : ../tokens.md#lifetimes-and-loop-labels
143
136
137
+ [ _LifetimeParam_ ] : #generic-parameters
144
138
[ _LifetimeBounds_ ] : ../trait-bounds.md
145
139
[ _Lifetime_ ] : ../trait-bounds.md
146
140
[ _OuterAttribute_ ] : ../attributes.md
@@ -150,6 +144,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
150
144
[ arrays ] : ../types/array.md
151
145
[ const contexts ] : ../const_eval.md#const-context
152
146
[ function pointers ] : ../types/function-pointer.md
147
+ [ higher-ranked lifetimes ] : ../trait-bounds.md#higher-ranked-trait-bounds
153
148
[ raw pointers ] : ../types/pointer.md#raw-pointers-const-and-mut
154
149
[ references ] : ../types/pointer.md#shared-references-
155
150
[ repeat expressions ] : ../expressions/array-expr.md
0 commit comments