1- # Type and Lifetime Parameters
1+ # Generic parameters
22
33> ** <sup >Syntax</sup >** \
44> _ Generics_ :\
55>   ;  ; ` < ` _ GenericParams_ ` > `
66>
77> _ GenericParams_ :\
88>   ;  ;   ;  ; _ LifetimeParams_ \
9- >   ;  ; | ( _ LifetimeParam_ ` , ` )<sup >\* </sup > _ TypeParams_
9+ >   ;  ; | ( _ LifetimeParam_ ` , ` )<sup >\* </sup > _ TypeParams_ \
10+ >   ;  ; | ( _ LifetimeParam_ ` , ` )<sup >\* </sup > ( _ TypeParam_ ` , ` )<sup >\* </sup > _ ConstParams_
1011>
1112> _ LifetimeParams_ :\
1213>   ;  ; ( _ LifetimeParam_ ` , ` )<sup >\* </sup > _ LifetimeParam_ <sup >?</sup >
1819>   ;  ; ( _ TypeParam_ ` , ` )<sup >\* </sup > _ TypeParam_ <sup >?</sup >
1920>
2021> _ 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_ ]
2229
2330Functions, 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 >,
2633usually immediately after the name of the item and before its definition. For
2734implementations, 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:
3038
3139``` rust
3240fn foo <'a , T >() {}
3341trait A <U > {}
3442struct 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>;
3571```
3672
3773[ References] , [ raw pointers] , [ arrays] , [ slices] [ arrays ] , [ tuples] , and
@@ -55,7 +91,7 @@ referred to with path syntax.
5591>   ;  ; _ ForLifetimes_ <sup >?</sup > [ _ Type_ ] ` : ` [ _ TypeParamBounds_ ] <sup >?</sup >
5692>
5793> _ ForLifetimes_ :\
58- >   ;  ; ` for ` ` < ` [ _ LifetimeParams_ ] ( #type-and-lifetime -parameters ) ` > `
94+ >   ;  ; ` for ` ` < ` [ _ LifetimeParams_ ] ( #generic -parameters ) ` > `
5995
6096* Where clauses* provide another way to specify bounds on type and lifetime
6197parameters 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
65101checked when the item is defined. It is an error for such a bound to be false.
66102
67103[ ` 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
69105bound on a mutable reference, [ trait object] or [ slice] [ arrays ] or ` Sized ` as a
70106bound on a trait object or slice.
71107
@@ -112,12 +148,15 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
112148[ _TypeParamBounds_ ] : ../trait-bounds.md
113149
114150[ arrays ] : ../types/array.md
151+ [ const contexts ] : ../const_eval.md#const-context
115152[ function pointers ] : ../types/function-pointer.md
116- [ references ] : ../types/pointer.md#shared-references-
117153[ raw pointers ] : ../types/pointer.md#raw-pointers-const-and-mut
154+ [ references ] : ../types/pointer.md#shared-references-
155+ [ repeat expressions ] : ../expressions/array-expr.md
118156[ `Clone` ] : ../special-types-and-traits.md#clone
119157[ `Copy` ] : ../special-types-and-traits.md#copy
120158[ `Sized` ] : ../special-types-and-traits.md#sized
121159[ tuples ] : ../types/tuple.md
122160[ trait object ] : ../types/trait-object.md
161+ [ types ] : ../types.md
123162[ attributes ] : ../attributes.md
0 commit comments