@@ -34,12 +34,6 @@ usually immediately after the name of the item and before its definition. For
34
34
implementations, which don't have a name, they come directly after ` impl ` .
35
35
The order of generic parameters is restricted to lifetime parameters, then type parameters, and then const parameters.
36
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
37
Some examples of items with type, const, and lifetime parameters:
44
38
45
39
``` rust
@@ -49,6 +43,33 @@ struct Ref<'a, T> where T: 'a { r: &'a T }
49
43
struct InnerArray <T , const N : usize >([T ; N ]);
50
44
```
51
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
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
+
52
73
[ References] , [ raw pointers] , [ arrays] , [ slices] [ arrays ] , [ tuples] , and
53
74
[ function pointers] have lifetime or type parameters as well, but are not
54
75
referred to with path syntax.
0 commit comments