- Notifications
You must be signed in to change notification settings - Fork 563
document const generics #901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
1bd2340 1c55d27 fec814c ce44ec2 7c659ad 49f9b34 210191a bd714f6 9fe55ae 4ec984b 15ec5a6 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| # Type and Lifetime Parameters | ||
| # Generic parameters | ||
| | ||
| > **<sup>Syntax</sup>**\ | ||
| > _Generics_ :\ | ||
| > `<` _GenericParams_ `>` | ||
| > | ||
| > _GenericParams_ :\ | ||
| > _LifetimeParams_\ | ||
| > | ( _LifetimeParam_ `,` )<sup>\*</sup> _TypeParams_ | ||
| > | ( _LifetimeParam_ `,` )<sup>\*</sup> _TypeParams_\ | ||
| > | ( _LifetimeParam_ `,` )<sup>\*</sup> ( _TypeParam_ `,` )<sup>\*</sup> _ConstParams_ | ||
| Comment on lines +9 to +10 Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this grammar is no longer correct (see #785). The order of the parameters doesn't matter syntactically. You don't have to fix it if you don't want to (although that would be nice), I just wanted to make a note of it. Contributor Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. won't do this in that PR, as I don't know what it's expected to look like now. | ||
| > | ||
| > _LifetimeParams_ :\ | ||
| > ( _LifetimeParam_ `,` )<sup>\*</sup> _LifetimeParam_<sup>?</sup> | ||
| | @@ -18,20 +19,34 @@ | |
| > ( _TypeParam_ `,` )<sup>\*</sup> _TypeParam_<sup>?</sup> | ||
| > | ||
| > _TypeParam_ :\ | ||
| > [_OuterAttribute_]<sup>?</sup> [IDENTIFIER] ( `:` [_TypeParamBounds_]<sup>?</sup> )<sup>?</sup> ( `=` [_Type_] )<sup>?</sup> | ||
| > [_OuterAttribute_]<sup>?</sup> [IDENTIFIER]( `:` [_TypeParamBounds_]<sup>?</sup> )<sup>?</sup> ( `=` [_Type_] )<sup>?</sup> | ||
| > | ||
| > _ConstParams_:\ | ||
| > ( _ConstParam_ `,` )<sup>\*</sup> _ConstParam_<sup>?</sup> | ||
| > | ||
| > _ConstParam_:\ | ||
| > [_OuterAttribute_]<sup>?</sup> `const` [IDENTIFIER] `:` [_Type_] | ||
| | ||
| Functions, type aliases, structs, enumerations, unions, traits, and | ||
| implementations may be *parameterized* by types and lifetimes. These parameters | ||
| are listed in angle <span class="parenthetical">brackets (`<...>`)</span>, | ||
| implementations may be *parameterized* by types, constants and lifetimes. These | ||
lcnr marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| parameters are listed in angle <span class="parenthetical">brackets (`<...>`)</span>, | ||
| usually immediately after the name of the item and before its definition. For | ||
| implementations, which don't have a name, they come directly after `impl`. | ||
| Lifetime parameters must be declared before type parameters. Some examples of | ||
| items with type and lifetime parameters: | ||
| The order of generic parameters is restricted to lifetime parameters, then type parameters and then const parameters. | ||
lcnr marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| The only allowed types of const parameters are `u8`, `u16`, `u32`, `u64`, `u128`, `usize` | ||
| `i8`, `i16`, `i32`, `i64`, `i128`, `isize`, `char` and `bool`. | ||
| Const parameters may only be be used as standalone arguments inside | ||
| of [types] and [repeat expressions]. | ||
| They can be used freely outside of [const contexts]. | ||
| ||
| | ||
| Some examples of items with type, const and lifetime parameters: | ||
lcnr marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| ```rust | ||
| fn foo<'a, T>() {} | ||
| trait A<U> {} | ||
| struct Ref<'a, T> where T: 'a { r: &'a T } | ||
| struct InnerArray<T, const N: usize>([T; N]); | ||
| ``` | ||
| | ||
| [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 | |
| checked when the item is defined. It is an error for such a bound to be false. | ||
| | ||
| [`Copy`], [`Clone`], and [`Sized`] bounds are also checked for certain generic | ||
| types when defining the item. It is an error to have `Copy` or `Clone`as a | ||
| types when defining the item. It is an error to have `Copy` or `Clone` as a | ||
| bound on a mutable reference, [trait object] or [slice][arrays] or `Sized` as a | ||
| bound on a trait object or slice. | ||
| | ||
| | @@ -112,12 +127,15 @@ struct Foo<#[my_flexible_clone(unbounded)] H> { | |
| [_TypeParamBounds_]: ../trait-bounds.md | ||
| | ||
| [arrays]: ../types/array.md | ||
| [const contexts]: ../const_eval.md#const-context | ||
| [function pointers]: ../types/function-pointer.md | ||
| [references]: ../types/pointer.md#shared-references- | ||
| [raw pointers]: ../types/pointer.md#raw-pointers-const-and-mut | ||
| [references]: ../types/pointer.md#shared-references | ||
lcnr marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| [repeat expressions]: ../expressions/array-expr.md | ||
| [`Clone`]: ../special-types-and-traits.md#clone | ||
| [`Copy`]: ../special-types-and-traits.md#copy | ||
| [`Sized`]: ../special-types-and-traits.md#sized | ||
| [tuples]: ../types/tuple.md | ||
| [trait object]: ../types/trait-object.md | ||
| [types]: ../types.md | ||
| [attributes]: ../attributes.md | ||
Uh oh!
There was an error while loading. Please reload this page.