You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/types/generics/index.md
+99-93Lines changed: 99 additions & 93 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -364,62 +364,64 @@ Const parameters can be used anywhere a [const item] can be used, with the excep
364
364
4. As a parameter to any type used in the body of any functions in the item.
365
365
5. As a part of the type of any fields in the item.
366
366
367
-
```rust
368
-
// Examples where const generic parameters can be used.
369
-
370
-
// Used in the signature of the item itself.
371
-
fn foo<const N: usize>(arr: [i32; N]) {
372
-
// Used as a type within a function body.
373
-
let x: [i32; N];
374
-
// Used as an expression.
375
-
println!("{}", N * 2);
376
-
}
377
-
378
-
// Used as a field of a struct.
379
-
struct Foo<const N: usize>([i32; N]);
380
-
381
-
impl<const N: usize> Foo<N> {
382
-
// Used as an associated constant.
383
-
const CONST: usize = N * 4;
384
-
}
385
-
386
-
trait Trait {
387
-
type Output;
388
-
}
389
-
390
-
impl<const N: usize> Trait for Foo<N> {
391
-
// Used as an associated type.
392
-
type Output = [i32; N];
393
-
}
394
-
```
395
-
396
-
```rust,compile_fail
397
-
// Examples where const generic parameters cannot be used.
398
-
fn foo<const N: usize>() {
399
-
// Cannot use in item definitions within a function body.
400
-
const BAD_CONST: [usize; N] = [1; N];
401
-
static BAD_STATIC: [usize; N] = [1; N];
402
-
fn inner(bad_arg: [usize; N]) {
403
-
let bad_value = N * 2;
404
-
}
405
-
type BadAlias = [usize; N];
406
-
struct BadStruct([usize; N]);
407
-
}
408
-
```
367
+
> [!EXAMPLE]
368
+
> ```rust
369
+
> // Examples where const generic parameters can be used.
370
+
>
371
+
> // Used in the signature of the item itself.
372
+
> fn foo<const N: usize>(arr: [i32; N]) {
373
+
> // Used as a type within a function body.
374
+
> let x: [i32; N];
375
+
> // Used as an expression.
376
+
> println!("{}", N * 2);
377
+
> }
378
+
>
379
+
> // Used as a field of a struct.
380
+
> struct Foo<const N: usize>([i32; N]);
381
+
>
382
+
> impl<const N: usize> Foo<N> {
383
+
> // Used as an associated constant.
384
+
> const CONST: usize = N * 4;
385
+
> }
386
+
>
387
+
> trait Trait {
388
+
> type Output;
389
+
> }
390
+
>
391
+
> impl<const N: usize> Trait for Foo<N> {
392
+
> // Used as an associated type.
393
+
> type Output = [i32; N];
394
+
> }
395
+
> ```
396
+
>
397
+
> ```rust,compile_fail
398
+
> // Examples where const generic parameters cannot be used.
399
+
> fn foo<const N: usize>() {
400
+
> // Cannot use in item definitions within a function body.
401
+
> const BAD_CONST: [usize; N] = [1; N];
402
+
> static BAD_STATIC: [usize; N] = [1; N];
403
+
> fn inner(bad_arg: [usize; N]) {
404
+
> let bad_value = N * 2;
405
+
> }
406
+
> type BadAlias = [usize; N];
407
+
> struct BadStruct([usize; N]);
408
+
> }
409
+
> ```
409
410
410
411
r[generics.const.standalone]
411
412
As a further restriction, const parameters may only appear as a standalone argument inside of a [type] or [array repeat expression]. In those contexts, they may only be used as a single segment [path expression], possibly inside a [block] (such as `N` or `{N}`). That is, they cannot be combined with other expressions.
412
413
413
-
```rust,compile_fail
414
-
// Examples where const parameters may not be used.
415
-
416
-
// Not allowed to combine in other expressions in types, such as the
> // Similarly not allowed for array repeat expressions.
422
+
> [1; {N + 1}]
423
+
> }
424
+
> ```
423
425
424
426
r[generics.const.inferred]
425
427
Where a const argument is expected, an `_` (optionally surrounded by any number of matching parentheses), called the *inferred const* ([generic argument rules][generics.arguments.complex-const-params], [array expression rules][expr.array.length-restriction]), can be used instead. This asks the compiler to infer the const argument if possible based on surrounding information.
Unlike type and lifetime parameters, const parameters can be declared without being used inside of a parameterized item, with the exception of implementations as described in [generic implementations]:
455
457
456
-
```rust,compile_fail
457
-
// ok
458
-
struct Foo<const N: usize>;
459
-
enum Bar<const M: usize> { A, B }
460
-
461
-
// ERROR: unused parameter
462
-
struct Baz<T>;
463
-
struct Biz<'a>;
464
-
struct Unconstrained;
465
-
impl<const N: usize> Unconstrained {}
466
-
```
458
+
> [!EXAMPLE]
459
+
> ```rust,compile_fail
460
+
> // ok
461
+
> struct Foo<const N: usize>;
462
+
> enum Bar<const M: usize> { A, B }
463
+
>
464
+
> // ERROR: unused parameter
465
+
> struct Baz<T>;
466
+
> struct Biz<'a>;
467
+
> struct Unconstrained;
468
+
> impl<const N: usize> Unconstrained {}
469
+
> ```
467
470
468
471
r[generics.const.exhaustiveness]
469
472
When resolving a trait bound obligation, the exhaustiveness of all implementations of const parameters is not considered when determining if the bound is satisfied. For example, in the following, even though all possible const values for the `bool` type are implemented, it is still an error that the trait bound is not satisfied:
470
473
471
-
```rust,compile_fail
472
-
struct Foo<const B: bool>;
473
-
trait Bar {}
474
-
impl Bar for Foo<true> {}
475
-
impl Bar for Foo<false> {}
476
-
477
-
fn needs_bar(_: impl Bar) {}
478
-
fn generic<const B: bool>() {
479
-
let v = Foo::<B>;
480
-
needs_bar(v); // ERROR: trait bound `Foo<B>: Bar` is not satisfied
481
-
}
482
-
```
474
+
> [!EXAMPLE]
475
+
> ```rust,compile_fail
476
+
> struct Foo<const B: bool>;
477
+
> trait Bar {}
478
+
> impl Bar for Foo<true> {}
479
+
> impl Bar for Foo<false> {}
480
+
>
481
+
> fn needs_bar(_: impl Bar) {}
482
+
> fn generic<const B: bool>() {
483
+
> let v = Foo::<B>;
484
+
> needs_bar(v); // ERROR: trait bound `Foo<B>: Bar` is not satisfied
485
+
> }
486
+
> ```
483
487
484
488
r[generics.const.arguments]
485
489
### Const arguments
@@ -525,15 +529,16 @@ When there is ambiguity if a generic argument could be resolved as either a type
525
529
526
530
<!-- TODO: Rewrite the paragraph above to be in terms of namespaces, once namespaces are introduced, and it is clear which namespace each parameter lives in. -->
527
531
528
-
```rust,compile_fail
529
-
typeN=u32;
530
-
structFoo<constN:usize>;
531
-
// The following is an error, because `N` is interpreted as the type alias `N`.
0 commit comments