Skip to content

Commit 210191a

Browse files
committed
generics
1 parent 49f9b34 commit 210191a

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/items/generics.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ usually immediately after the name of the item and before its definition. For
3434
implementations, which don't have a name, they come directly after `impl`.
3535
The order of generic parameters is restricted to lifetime parameters, then type parameters, and then const parameters.
3636

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-
4337
Some examples of items with type, const, and lifetime parameters:
4438

4539
```rust
@@ -49,6 +43,33 @@ struct Ref<'a, T> where T: 'a { r: &'a T }
4943
struct InnerArray<T, const N: usize>([T; N]);
5044
```
5145

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
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
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>;
71+
```
72+
5273
[References], [raw pointers], [arrays], [slices][arrays], [tuples], and
5374
[function pointers] have lifetime or type parameters as well, but are not
5475
referred to with path syntax.

0 commit comments

Comments
 (0)