Skip to content

Commit 427fb4d

Browse files
authored
Merge pull request #1053 from tmandry/patch-1
Add blog post announcing Rust 1.66.0
2 parents 60bfdfa + 3d954a3 commit 427fb4d

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

posts/2022-12-15-Rust-1.66.0.md

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
---
2+
layout: post
3+
title: "Announcing Rust 1.66.0"
4+
author: The Rust Release Team
5+
release: true
6+
---
7+
8+
The Rust team is happy to announce a new version of Rust, 1.66.0. Rust is a
9+
programming language empowering everyone to build reliable and efficient
10+
software.
11+
12+
If you have a previous version of Rust installed via rustup, you can get 1.66.0
13+
with:
14+
15+
```console
16+
rustup update stable
17+
```
18+
19+
If you don't have it already, you can [get
20+
`rustup`](https://www.rust-lang.org/install.html) from the appropriate page on
21+
our website, and check out the [detailed release notes for
22+
1.66.0](https://github.com/rust-lang/rust/blob/stable/RELEASES.md#version-1660-2022-12-15)
23+
on GitHub.
24+
25+
If you'd like to help us out by testing future releases, you might consider
26+
updating locally to use the beta channel (`rustup default beta`) or the nightly
27+
channel (`rustup default nightly`). Please
28+
[report](https://github.com/rust-lang/rust/issues/new/choose) any bugs you
29+
might come across!
30+
31+
## What's in 1.66.0 stable
32+
33+
### Explicit discriminants on enums with fields
34+
35+
Enums with integer representations can now use explicit discriminants, even when they have fields.
36+
37+
```rust
38+
#[repr(u8)]
39+
enum Foo {
40+
A(u8),
41+
B(i8),
42+
C(bool) = 42,
43+
}
44+
```
45+
46+
Previously, you could use explicit discriminants on enums with representations, but only if none of their variants had fields. Explicit discriminants are useful when passing values across language boundaries where the representation of the enum needs to match in both languages. For example,
47+
48+
```rust
49+
#[repr(u8)]
50+
enum Bar {
51+
A,
52+
B,
53+
C = 42,
54+
}
55+
```
56+
57+
Here the `Bar` enum is guaranteed to have the same layout as `u8`. Each variant will use either the specified discriminant value or default to starting with 0.
58+
59+
```rust
60+
assert_eq!(0, Bar::A as u8);
61+
assert_eq!(1, Bar::B as u8);
62+
assert_eq!(42, Bar::C as u8);
63+
```
64+
65+
You could even add fields to enums with `#[repr(Int)]`, and they would be laid out in a predictable way. Previously, however, you could not use these features together. That meant that making `Foo::C`'s discriminant equal to 42 as above would be harder to achieve. You would need to add 41 hidden variants in between as a workaround with implicit discriminants!
66+
67+
Starting in Rust 1.66.0, the above example compiles, allowing you to use explicit discriminants on any enum with a `#[repr(Int)]` attribute.
68+
69+
### `core::hint::black_box`
70+
71+
When benchmarking or examining the machine code produced by a compiler, it's often useful to prevent optimizations from occurring in certain places. In the following example, the function `push_cap` executes `Vec::push` 4 times in a loop:
72+
73+
```rust
74+
fn push_cap(v: &mut Vec<i32>) {
75+
for i in 0..4 {
76+
v.push(i);
77+
}
78+
}
79+
80+
pub fn bench_push() -> Duration {
81+
let mut v = Vec::with_capacity(4);
82+
let now = Instant::now();
83+
push_cap(&mut v);
84+
now.elapsed()
85+
}
86+
```
87+
88+
If you inspect the optimized output of the compiler on x86_64, you'll notice that it looks rather short:
89+
90+
```asm
91+
example::bench_push:
92+
sub rsp, 24
93+
call qword ptr [rip + std::time::Instant::now@GOTPCREL]
94+
lea rdi, [rsp + 8]
95+
mov qword ptr [rsp + 8], rax
96+
mov dword ptr [rsp + 16], edx
97+
call qword ptr [rip + std::time::Instant::elapsed@GOTPCREL]
98+
add rsp, 24
99+
ret
100+
```
101+
102+
In fact, the entire function `push_cap` we wanted to benchmark has been optimized away!
103+
104+
We can work around this using the newly stabilized `black_box` function. Functionally, `black_box` is not very interesting: it takes the value you pass it and passes it right back. Internally, however, the compiler treats `black_box` as a function that could do anything with its input and return any value (as its name implies).
105+
106+
This is very useful for disabling optimizations like the one we see above. For example, we can hint to the compiler that the vector will actually be used for something after every iteration of the for loop.
107+
108+
```rust
109+
use std::hint::black_box;
110+
111+
fn push_cap(v: &mut Vec<i32>) {
112+
for i in 0..4 {
113+
v.push(i);
114+
black_box(v.as_ptr());
115+
}
116+
}
117+
```
118+
119+
Now we can find the unrolled for loop in our [optimized assembly output](https://rust.godbolt.org/z/Ws1GGbY6Y):
120+
121+
```asm
122+
mov dword ptr [rbx], 0
123+
mov qword ptr [rsp + 8], rbx
124+
mov dword ptr [rbx + 4], 1
125+
mov qword ptr [rsp + 8], rbx
126+
mov dword ptr [rbx + 8], 2
127+
mov qword ptr [rsp + 8], rbx
128+
mov dword ptr [rbx + 12], 3
129+
mov qword ptr [rsp + 8], rbx
130+
```
131+
132+
You can also see a side effect of calling `black_box` in this assembly output. The instruction `mov qword ptr [rsp + 8], rbx` is uselessly repeated after every iteration. This instruction writes the address `v.as_ptr()` as the first argument of the function, which is never actually called.
133+
134+
Notice that the generated code is not at all concerned with the possibility of allocations introduced by the `push` call. This is because the compiler is still using the fact that we called `Vec::with_capacity(4)` in the `bench_push` function. You can play around with the placement of `black_box`, or try using it in multiple places, to see its effects on compiler optimizations.
135+
136+
### cargo remove
137+
138+
In Rust 1.62.0 we introduced `cargo add`, a command line utility to add dependencies to your project. Now you can use `cargo remove` to remove dependencies.
139+
140+
### Stabilized APIs
141+
142+
- [`proc_macro::Span::source_text`](https://doc.rust-lang.org/stable/proc_macro/struct.Span.html#method.source_text)
143+
- [`u*::{checked_add_signed, overflowing_add_signed, saturating_add_signed, wrapping_add_signed}`](https://doc.rust-lang.org/stable/std/primitive.u8.html#method.checked_add_signed)
144+
- [`i*::{checked_add_unsigned, overflowing_add_unsigned, saturating_add_unsigned, wrapping_add_unsigned}`](https://doc.rust-lang.org/stable/std/primitive.i8.html#method.checked_add_unsigned)
145+
- [`i*::{checked_sub_unsigned, overflowing_sub_unsigned, saturating_sub_unsigned, wrapping_sub_unsigned}`](https://doc.rust-lang.org/stable/std/primitive.i8.html#method.checked_sub_unsigned)
146+
- [`BTreeSet::{first, last, pop_first, pop_last}`](https://doc.rust-lang.org/stable/std/collections/struct.BTreeSet.html#method.first)
147+
- [`BTreeMap::{first_key_value, last_key_value, first_entry, last_entry, pop_first, pop_last}`](https://doc.rust-lang.org/stable/std/collections/struct.BTreeMap.html#method.first_key_value)
148+
- [Add `AsFd` implementations for stdio lock types on WASI.](https://github.com/rust-lang/rust/pull/101768/)
149+
- [`impl TryFrom<Vec<T>> for Box<[T; N]>`](https://doc.rust-lang.org/stable/std/boxed/struct.Box.html#impl-TryFrom%3CVec%3CT%2C%20Global%3E%3E-for-Box%3C%5BT%3B%20N%5D%2C%20Global%3E)
150+
- [`core::hint::black_box`](https://doc.rust-lang.org/stable/std/hint/fn.black_box.html)
151+
- [`Duration::try_from_secs_{f32,f64}`](https://doc.rust-lang.org/stable/std/time/struct.Duration.html#method.try_from_secs_f32)
152+
- [`Option::unzip`](https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.unzip)
153+
- [`std::os::fd`](https://doc.rust-lang.org/stable/std/os/fd/index.html)
154+
155+
### Other changes
156+
157+
There are other changes in the Rust 1.66 release, including:
158+
159+
- You can now use `..=X` ranges in patterns.
160+
- Linux builds now optimize the rustc frontend and LLVM backend with LTO and BOLT, respectively, improving both runtime performance and memory usage.
161+
162+
Check out everything that changed in
163+
[Rust](https://github.com/rust-lang/rust/blob/stable/RELEASES.md#version-1660-2022-12-15),
164+
[Cargo](https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md#cargo-166-2022-12-15),
165+
and [Clippy](https://github.com/rust-lang/rust-clippy/blob/master/CHANGELOG.md#rust-166).
166+
167+
### Contributors to 1.66.0
168+
169+
Many people came together to create Rust 1.66.0.
170+
We couldn't have done it without all of you.
171+
[Thanks!](https://thanks.rust-lang.org/rust/1.66.0/)

0 commit comments

Comments
 (0)