Skip to content

Commit 59f05a0

Browse files
committed
docs(unstable-book): Document const generics features
1 parent 22cc674 commit 59f05a0

4 files changed

Lines changed: 278 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# generic_const_args
2+
3+
Allows using generics in more complex const expressions, based on definitional equality.
4+
5+
The tracking issue for this feature is: [#151972]
6+
7+
[#151972]: https://github.com/rust-lang/rust/issues/151972
8+
9+
------------------------
10+
11+
Warning: This feature is incomplete; its design and syntax may change.
12+
13+
This feature enables many of the same use cases supported by [generic_const_exprs],
14+
but based on the machinery developed for [min_generic_const_args]. In a way, it is
15+
meant to be an interim successor for GCE (though it might not currently support all
16+
the valid cases that supported by GCE).
17+
18+
See also: [generic_const_items]
19+
20+
[min_generic_const_args]: min-generic-const-args.md
21+
[generic_const_exprs]: generic-const-exprs.md
22+
[generic_const_items]: generic-const-items.md
23+
24+
## Examples
25+
26+
```rust
27+
#![feature(generic_const_items)]
28+
#![feature(min_generic_const_args)]
29+
#![feature(generic_const_args)]
30+
#![expect(incomplete_features)]
31+
32+
type const ADD1<const N: usize>: usize = const { N + 1 };
33+
34+
type const INC<const N: usize>: usize = ADD1::<N>;
35+
36+
const ARR: [(); ADD1::<0>] = [(); INC::<0>];
37+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# generic_const_exprs
2+
3+
Allows non-trivial generic constants which have to have wfness manually propagated to callers
4+
5+
The tracking issue for this feature is: [#76560]
6+
7+
8+
[#76560]: https://github.com/rust-lang/rust/issues/76560
9+
10+
------------------------
11+
12+
Warning: This feature is incomplete; its design and syntax may change.
13+
14+
See also: [min_generic_const_args], [generic_const_args]
15+
16+
[min_generic_const_args]: min-generic-const-args.md
17+
[generic_const_args]: generic-const-args.md
18+
19+
## Examples
20+
21+
```rust
22+
#![allow(incomplete_features)]
23+
#![feature(generic_const_exprs)]
24+
25+
// Use parameters that depend on a generic argument.
26+
struct Foo<const N: usize>
27+
where
28+
[(); N + 1]:,
29+
{
30+
array: [usize; N + 1],
31+
}
32+
33+
// Use generic parameters in const operations.
34+
trait Bar {
35+
const X: usize;
36+
const Y: usize;
37+
}
38+
39+
// Note `B::X * B::Y`.
40+
const fn baz<B: Bar>(x: [usize; B::X], y: [usize; B::Y]) -> [usize; B::X * B::Y] {
41+
let mut out = [0; B::X * B::Y];
42+
let mut i = 0;
43+
while i < B::Y {
44+
let mut j = 0;
45+
while j < B::X {
46+
out[i * B::X + j] = y[i].saturating_mul(x[j]);
47+
j += 1;
48+
}
49+
i += 1;
50+
}
51+
out
52+
}
53+
54+
55+
// Create a new type based on a generic argument.
56+
pub struct Grow<const N: usize> {
57+
arr: [usize; N],
58+
}
59+
60+
impl<const N: usize> Grow<N> {
61+
pub const fn grow(self, val: usize) -> Grow<{ N + 1 }> {
62+
let mut new_arr = [0; { N + 1 }];
63+
let mut idx = 0;
64+
while idx < N {
65+
new_arr[idx] = self.arr[idx];
66+
idx += 1;
67+
}
68+
new_arr[N] = val;
69+
Grow { arr: new_arr }
70+
}
71+
}
72+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# generic_const_items
2+
3+
Allows generic parameters and where-clauses on free & associated const items.
4+
5+
The tracking issue for this feature is: [#113521]
6+
7+
[#113521]: https://github.com/rust-lang/rust/issues/113521
8+
9+
------------------------
10+
11+
Warning: This feature is an [experiment] and lacks an RFC.
12+
There are no guarantees that it will ever be stabilized.
13+
14+
See also: [generic_const_exprs], [min_generic_const_args].
15+
16+
[experiment]: https://lang-team.rust-lang.org/how_to/experiment.html
17+
[generic_const_exprs]: generic-const-exprs.md
18+
[min_generic_const_args]: min-generic-const-args.md
19+
20+
## Examples
21+
22+
### Generic constant values
23+
24+
```rust
25+
#![allow(incomplete_features)]
26+
#![feature(generic_const_items)]
27+
28+
const GENERIC_VAL<const ARG: usize>: usize = const { ARG + 1 };
29+
30+
#[test]
31+
fn generic_const_arg() {
32+
assert_eq!(GENERIC_VAL::<1>, 2);
33+
assert_eq!(GENERIC_VAL::<2>, 3);
34+
}
35+
```
36+
37+
### Conditional constants
38+
39+
```rust
40+
#![allow(incomplete_features)]
41+
#![feature(generic_const_items)]
42+
43+
// `GENERIC_VAL::<0>` will fail to compile
44+
const GENERIC_VAL<const ARG: usize>: usize = const {
45+
if ARG > 0 {
46+
ARG + 1
47+
} else {
48+
panic!("0 value")
49+
}
50+
};
51+
52+
// Will fail to compile if the `Copy` derive is removed.
53+
const COPY_MARKER<C: Copy>: () = ();
54+
55+
#[derive(Clone, Copy)]
56+
struct Foo;
57+
58+
const FOO_IS_COPY: () = COPY_MARKER::<Foo>;
59+
```
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# min_generic_const_args
2+
3+
Enables the generic const args MVP.
4+
5+
The tracking issue for this feature is: [#132980]
6+
7+
[#132980]: https://github.com/rust-lang/rust/issues/132980
8+
9+
------------------------
10+
11+
Warning: This feature is incomplete; its design and syntax may change.
12+
13+
This feature acts as a minimal alternative to [generic_const_exprs] that allows a smaller subset of functionality,
14+
and uses a different approach for implementation. It is intentionally more restrictive, which helps with avoiding edge
15+
cases that make the `generic_const_exprs` hard to implement properly. See [Feature background][feature_background]
16+
for more details.
17+
18+
Related features: [generic_const_args], [generic_const_items].
19+
20+
[feature_background]: https://hackmd.io/OsVAAEgOS2ajDwxg44kN-A?view
21+
[generic_const_exprs]: generic-const-exprs.md
22+
[generic_const_args]: generic-const-args.md
23+
[generic_const_items]: generic-const-items.md
24+
25+
## `type const` syntax
26+
27+
This feature introduces new syntax: `type const`.
28+
Constants marked as `type const` are allowed to be used in type contexts, e.g.:
29+
30+
```rust
31+
#![allow(incomplete_features)]
32+
#![feature(min_generic_const_args)]
33+
34+
type const X: usize = 1;
35+
const Y: usize = 1;
36+
37+
struct Foo {
38+
good_arr: [(); X], // Allowed
39+
bad_arr: [(); Y], // Will not compile, `Y` must be `type const`.
40+
}
41+
```
42+
43+
## Examples
44+
45+
```rust
46+
#![allow(incomplete_features)]
47+
#![feature(min_generic_const_args)]
48+
49+
trait Bar {
50+
type const VAL: usize;
51+
type const VAL2: usize;
52+
}
53+
54+
struct Baz;
55+
56+
impl Bar for Baz {
57+
type const VAL: usize = 2;
58+
type const VAL2: usize = const { Self::VAL * 2 };
59+
}
60+
61+
struct Foo<B: Bar> {
62+
arr1: [usize; B::VAL],
63+
arr2: [usize; B::VAL2],
64+
}
65+
```
66+
67+
Note that with [generic_const_exprs] the same example would look as follows:
68+
69+
```rust
70+
#![allow(incomplete_features)]
71+
#![feature(generic_const_exprs)]
72+
73+
trait Bar {
74+
const VAL: usize;
75+
const VAL2: usize;
76+
}
77+
78+
struct Baz;
79+
80+
impl Bar for Baz {
81+
const VAL: usize = 2;
82+
const VAL2: usize = const { Self::VAL * 2 };
83+
}
84+
85+
struct Foo<B: Bar>
86+
where
87+
[(); B::VAL]:,
88+
[(); B::VAL2]:,
89+
{
90+
arr1: [usize; B::VAL],
91+
arr2: [usize; B::VAL2],
92+
}
93+
```
94+
95+
Use of const functions is allowed:
96+
97+
```rust
98+
#![allow(incomplete_features)]
99+
#![feature(min_generic_const_args)]
100+
101+
const VAL: usize = 1;
102+
103+
const fn inc(val: usize) -> usize {
104+
val + 1
105+
}
106+
107+
type const INC: usize = const { inc(VAL) };
108+
109+
const ARR: [usize; INC] = [0; INC];
110+
```

0 commit comments

Comments
 (0)