Skip to content

Commit ca4eeaa

Browse files
committed
const generics ftw
1 parent 010747d commit ca4eeaa

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

posts/2020-10-05-Rust-1.47.md

+50-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,59 @@ appropriate page on our website, and check out the [detailed release notes for
2525

2626
## What's in 1.47.0 stable
2727

28-
This release contains no new significant features, and is mostly quality of
29-
life improvements, library stabilizations and const-ifications, and toolchain
28+
This release contains no new language features, though it does add one
29+
long-awaited standard library feature. It is mostly quality of life
30+
improvements, library stabilizations and const-ifications, and toolchain
3031
improvements. See the [detailed release notes][notes] to learn about other
3132
changes not covered by this post.
3233

34+
#### Traits on larger arrays
35+
36+
Rust does not currently have a way to be generic over integer values. This
37+
has long caused problems with arrays, because arrays have an integer as part
38+
of their type; `[T; N]` is the type of an array of type `T` of `N` length.
39+
Because there is no way to be generic over `N`, you have to manually implement
40+
traits for arrays for every `N` you want to support. For the standard library,
41+
it was decided to support up to `N` of 32.
42+
43+
We have been working on a feature called "const generics" that would allow
44+
you to be generic over `N`. Fully explaining this feature is out of the scope
45+
of this post, because we are not stabilizing const generics just yet.
46+
However, the core of this feature has been implemented in the compiler, and
47+
it has been decided that the feature is far enough along that we are okay
48+
with [the standard library using it to implement traits on arrays of any
49+
length](https://github.com/rust-lang/rust/pull/74060/). What this means in
50+
practice is that if you try to do something like this on Rust 1.46:
51+
52+
```rust
53+
fn main() {
54+
let xs = [0; 34];
55+
56+
println!("{:?}", xs);
57+
}
58+
```
59+
60+
you'd get this error:
61+
62+
```text
63+
error[E0277]: arrays only have std trait implementations for lengths 0..=32
64+
--> src/main.rs:4:22
65+
|
66+
4 | println!("{:?}", xs);
67+
| ^^ the trait `std::array::LengthAtMost32` is not implemented for `[{integer}; 34]`
68+
|
69+
= note: required because of the requirements on the impl of `std::fmt::Debug` for `[{integer}; 34]`
70+
= note: required by `std::fmt::Debug::fmt`
71+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
72+
```
73+
74+
But with Rust 1.47, it will properly print out.
75+
76+
This should make arrays significantly more useful to folks, though it will
77+
take until the const generics feature stabilizes for libraries to fully be
78+
able to do this kind of implementation for their own traits. We do not have
79+
a current estimated date for the stabilization of const generics.
80+
3381
#### Shorter backtraces
3482

3583
Back in Rust 1.18, we [made some changes to the backtraces `rustc` would

0 commit comments

Comments
 (0)