Skip to content

Commit 1bd2340

Browse files
committed
mention const generics
1 parent 0eb434d commit 1bd2340

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/const_eval.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ also constant expressions and do not cause any [`Drop::drop`][destructors] calls
2020
to be run.
2121

2222
* [Literals].
23+
* [Const parameters].
2324
* [Paths] to [functions] and [constants].
2425
Recursively defining constants is not allowed.
2526
* Paths to [statics]. These are only allowed within the initializer of a static.
@@ -112,6 +113,7 @@ Conversely, the following are possible in a const function, but not in a const c
112113
[comparison]: expressions/operator-expr.md#comparison-operators
113114
[const functions]: items/functions.md#const-functions
114115
[constants]: items/constant-items.md
116+
[Const parameters]: items/generics.md
115117
[dereference operator]: expressions/operator-expr.md#the-dereference-operator
116118
[destructors]: destructors.md
117119
[enum discriminants]: items/enumerations.md#custom-discriminant-values-for-fieldless-enumerations

src/items/generics.md

+27-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
# Type and Lifetime Parameters
1+
# Generic parameters
22

33
> **<sup>Syntax</sup>**\
44
> _Generics_ :\
55
> &nbsp;&nbsp; `<` _GenericParams_ `>`
66
>
77
> _GenericParams_ :\
88
> &nbsp;&nbsp; &nbsp;&nbsp; _LifetimeParams_\
9-
> &nbsp;&nbsp; | ( _LifetimeParam_ `,` )<sup>\*</sup> _TypeParams_
9+
> &nbsp;&nbsp; | ( _LifetimeParam_ `,` )<sup>\*</sup> _TypeParams_\
10+
> &nbsp;&nbsp; | ( _LifetimeParam_ `,` )<sup>\*</sup> ( _TypeParam_ `,` )<sup>\*</sup> _ConstParams_
1011
>
1112
> _LifetimeParams_ :\
1213
> &nbsp;&nbsp; ( _LifetimeParam_ `,` )<sup>\*</sup> _LifetimeParam_<sup>?</sup>
@@ -18,20 +19,34 @@
1819
> &nbsp;&nbsp; ( _TypeParam_ `,` )<sup>\*</sup> _TypeParam_<sup>?</sup>
1920
>
2021
> _TypeParam_ :\
21-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>?</sup> [IDENTIFIER] ( `:` [_TypeParamBounds_]<sup>?</sup> )<sup>?</sup> ( `=` [_Type_] )<sup>?</sup>
22+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>?</sup> [IDENTIFIER]( `:` [_TypeParamBounds_]<sup>?</sup> )<sup>?</sup> ( `=` [_Type_] )<sup>?</sup>
23+
>
24+
> _ConstParams_:\
25+
> &nbsp;&nbsp; ( _ConstParam_ `,` )<sup>\*</sup> _ConstParam_<sup>?</sup>
26+
>
27+
> _ConstParam_:\
28+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>?</sup> `const` [IDENTIFIER] `:` [_Type_]
2229
2330
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>,
2633
usually immediately after the name of the item and before its definition. For
2734
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:
3044

3145
```rust
3246
fn foo<'a, T>() {}
3347
trait A<U> {}
3448
struct Ref<'a, T> where T: 'a { r: &'a T }
49+
struct InnerArray<T, const N: usize>([T; N]);
3550
```
3651

3752
[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
6580
checked when the item is defined. It is an error for such a bound to be false.
6681

6782
[`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
6984
bound on a mutable reference, [trait object] or [slice][arrays] or `Sized` as a
7085
bound on a trait object or slice.
7186

@@ -112,12 +127,15 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
112127
[_TypeParamBounds_]: ../trait-bounds.md
113128

114129
[arrays]: ../types/array.md
130+
[const contexts]: ../const_eval.md#const-context
115131
[function pointers]: ../types/function-pointer.md
116-
[references]: ../types/pointer.md#shared-references-
117132
[raw pointers]: ../types/pointer.md#raw-pointers-const-and-mut
133+
[references]: ../types/pointer.md#shared-references
134+
[repeat expressions]: ../expressions/array-expr.md
118135
[`Clone`]: ../special-types-and-traits.md#clone
119136
[`Copy`]: ../special-types-and-traits.md#copy
120137
[`Sized`]: ../special-types-and-traits.md#sized
121138
[tuples]: ../types/tuple.md
122139
[trait object]: ../types/trait-object.md
140+
[types]: ../types.md
123141
[attributes]: ../attributes.md

0 commit comments

Comments
 (0)