Skip to content

Commit 772a85d

Browse files
committed
Announcing Rust 1.56.0 and Rust 2021
1 parent 2371b1d commit 772a85d

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

posts/2021-10-21-Rust-1.56.0.md

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
---
2+
layout: post
3+
title: "Announcing Rust 1.56.0 and Rust 2021"
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.56.0, and "Rust 2021" as well.
9+
Rust is a programming language empowering everyone to build reliable and efficient software.
10+
11+
If you have a previous version of Rust installed via rustup, getting Rust 1.56.0 is as easy as:
12+
13+
```console
14+
rustup update stable
15+
```
16+
17+
If you don't have it already, you can [get `rustup`][install]
18+
from the appropriate page on our website, and check out the
19+
[detailed release notes for 1.56.0][notes] on GitHub.
20+
21+
[install]: https://www.rust-lang.org/install.html
22+
[notes]: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1560-2021-10-21
23+
24+
## What's in 1.56.0 stable
25+
26+
### Rust 2021
27+
28+
We wrote about plans for Rust 2021 [in May](https://blog.rust-lang.org/2021/05/11/edition-2021.html).
29+
This is a smaller step for an edition, especially compared to 2018, but there
30+
are still some nice quality-of-life changes that require an edition opt-in to
31+
avoid breaking some corner cases in existing code. See the new chapters of the
32+
edition guide below for more details on each new feature and guidance for
33+
migration.
34+
35+
* [Additions to the prelude](https://doc.rust-lang.org/edition-guide/rust-2021/prelude.html): `TryInto`, `TryFrom`, and `FromIterator`.
36+
* [Default Cargo feature resolver](https://doc.rust-lang.org/edition-guide/rust-2021/default-cargo-resolver.html) is now version 2.
37+
* [`IntoIterator` for arrays](https://doc.rust-lang.org/edition-guide/rust-2021/IntoIterator-for-arrays.html) now includes `array.into_iter()` calls.
38+
* [Disjoint capture in closures](https://doc.rust-lang.org/edition-guide/rust-2021/disjoint-capture-in-closures.html) rather than always capturing whole identifiers.
39+
* [Panic macro consistency](https://doc.rust-lang.org/edition-guide/rust-2021/panic-macro-consistency.html) now always uses `format_args!(..)`, just like `println!()`.
40+
* [Reserving syntax](https://doc.rust-lang.org/edition-guide/rust-2021/reserving-syntax.html) for `ident#`, `ident"..."`, and `ident'...'`.
41+
* [Warnings promoted to errors](https://doc.rust-lang.org/edition-guide/rust-2021/warnings-promoted-to-error.html): `bare_trait_objects` and `ellipsis_inclusive_range_patterns`.
42+
* [Or patterns in macro-rules](https://doc.rust-lang.org/edition-guide/rust-2021/or-patterns-macro-rules.html) now match top-level `A|B` in `:pat`.
43+
44+
#### Disjoint capture in closures
45+
46+
Closures automatically capture values or references to identifiers that are
47+
used in the body, but before 2021, they were always captured in whole. The new
48+
disjoint-capture feature will likely simply the way you write closures, so
49+
let's look at a quick example:
50+
51+
```rust
52+
let a = SomeStruct::new();
53+
54+
drop(a.x); // Move out of one field of the struct
55+
56+
println!("{}", a.y); // Ok: Still use another field of the struct
57+
58+
let c = || println!("{}", a.y); // Error: Tries to capture all of `a`
59+
c();
60+
```
61+
62+
To fix this, you would have had to extract something like `let y = &a.y;`
63+
manually before the closure to limit its capture. Starting in Rust 2021,
64+
closures will automatically capture only the fields that they use, so the
65+
above example will compile fine!
66+
67+
This new behavior is only activated in the new edition, since it can change
68+
the order in which fields are dropped. As for all edition changes, an
69+
automatic migration is available, which will update your closures for which
70+
this matters. It can insert `let _ = &a;` inside the closure to force the
71+
entire struct to be captured as before.
72+
73+
#### Migrating to 2021
74+
75+
The guide includes migration instructions for all new features, and in general
76+
[transitioning an existing project to a new edition](https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html),
77+
In many cases `cargo fix` can automate the necessary changes. You may even
78+
find that no changes in your code are needed at all for 2021!
79+
80+
However small this edition appears on the surface, it's still the product
81+
of a lot of hard work from many contributors. Please join us in
82+
[celebration and thanks](https://github.com/rust-lang/rust/issues/88623)!
83+
84+
### Cargo `rust-version`
85+
86+
`Cargo.toml` now supports a `[package]` [`rust-version`] field to specify
87+
the minimum supported Rust version for a crate, and Cargo will exit with an
88+
early error if that is not satisfied. This doesn't currently influence the
89+
dependency resolver, but the idea is to catch compatibility problems before
90+
they turn into cryptic compiler errors.
91+
92+
[`rust-version`]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-rust-version-field
93+
94+
### New bindings in `binding @ pattern`
95+
96+
Rust pattern matching can be written with a single identifier that binds
97+
the entire value, followed by `@` and a more refined structural pattern,
98+
but this has not allowed additional bindings in that pattern -- until now!
99+
100+
```rust
101+
struct Matrix {
102+
data: Vec<f64>,
103+
row_len: usize,
104+
}
105+
106+
// Before, we need separate statements to bind
107+
// the whole struct and also read its parts.
108+
let matrix = get_matrix();
109+
let row_len = matrix.row_len;
110+
// or with a destructuring pattern:
111+
let Matrix { row_len, .. } = matrix;
112+
113+
// Rust 1.56 now lets you bind both at once!
114+
let matrix @ Matrix { row_len, .. } = get_matrix();
115+
```
116+
117+
This actually was allowed in the days before Rust 1.0, but that was removed
118+
due to known [unsoundness](https://github.com/rust-lang/rust/pull/16053) at
119+
the time. With the evolution of the borrow checker since that time, and with
120+
heavy testing, the compiler team determined that this was safe to finally
121+
allow in stable Rust!
122+
123+
### Stabilized APIs
124+
125+
The following methods and trait implementations were stabilized.
126+
127+
- [`std::os::unix::fs::chroot`]
128+
- [`UnsafeCell::raw_get`]
129+
- [`BufWriter::into_parts`]
130+
- [`core::panic::{UnwindSafe, RefUnwindSafe, AssertUnwindSafe}`]\
131+
\(previously only in `std`)
132+
- [`Vec::shrink_to`]
133+
- [`String::shrink_to`]
134+
- [`OsString::shrink_to`]
135+
- [`PathBuf::shrink_to`]
136+
- [`BinaryHeap::shrink_to`]
137+
- [`VecDeque::shrink_to`]
138+
- [`HashMap::shrink_to`]
139+
- [`HashSet::shrink_to`]
140+
141+
The following previously stable functions are now `const`.
142+
143+
- [`std::mem::transmute`]
144+
- [`[T]::first`][`slice::first`]
145+
- [`[T]::split_first`][`slice::split_first`]
146+
- [`[T]::last`][`slice::last`]
147+
- [`[T]::split_last`][`slice::split_last`]
148+
149+
[`std::os::unix::fs::chroot`]: https://doc.rust-lang.org/stable/std/os/unix/fs/fn.chroot.html
150+
[`UnsafeCell::raw_get`]: https://doc.rust-lang.org/stable/std/cell/struct.UnsafeCell.html#method.raw_get
151+
[`BufWriter::into_parts`]: https://doc.rust-lang.org/stable/std/io/struct.BufWriter.html#method.into_parts
152+
[`core::panic::{UnwindSafe, RefUnwindSafe, AssertUnwindSafe}`]: https://github.com/rust-lang/rust/pull/84662
153+
[`Vec::shrink_to`]: https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.shrink_to
154+
[`String::shrink_to`]: https://doc.rust-lang.org/stable/std/string/struct.String.html#method.shrink_to
155+
[`OsString::shrink_to`]: https://doc.rust-lang.org/stable/std/ffi/struct.OsString.html#method.shrink_to
156+
[`PathBuf::shrink_to`]: https://doc.rust-lang.org/stable/std/path/struct.PathBuf.html#method.shrink_to
157+
[`BinaryHeap::shrink_to`]: https://doc.rust-lang.org/stable/std/collections/struct.BinaryHeap.html#method.shrink_to
158+
[`VecDeque::shrink_to`]: https://doc.rust-lang.org/stable/std/collections/struct.VecDeque.html#method.shrink_to
159+
[`HashMap::shrink_to`]: https://doc.rust-lang.org/stable/std/collections/hash_map/struct.HashMap.html#method.shrink_to
160+
[`HashSet::shrink_to`]: https://doc.rust-lang.org/stable/std/collections/hash_set/struct.HashSet.html#method.shrink_to
161+
[`std::mem::transmute`]: https://doc.rust-lang.org/stable/std/mem/fn.transmute.html
162+
[`slice::first`]: https://doc.rust-lang.org/stable/std/primitive.slice.html#method.first
163+
[`slice::split_first`]: https://doc.rust-lang.org/stable/std/primitive.slice.html#method.split_first
164+
[`slice::last`]: https://doc.rust-lang.org/stable/std/primitive.slice.html#method.last
165+
[`slice::split_last`]: https://doc.rust-lang.org/stable/std/primitive.slice.html#method.split_last
166+
167+
### Other changes
168+
169+
There are other changes in the Rust 1.56.0 release: check out what changed in
170+
[Rust](https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1560-2021-10-21),
171+
[Cargo](https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md#cargo-156-2021-10-21),
172+
and [Clippy](https://github.com/rust-lang/rust-clippy/blob/master/CHANGELOG.md#rust-156).
173+
174+
### Contributors to 1.56.0
175+
176+
Many people came together to create Rust 1.56.0.
177+
We couldn't have done it without all of you.
178+
[Thanks!](https://thanks.rust-lang.org/rust/1.56.0/)

0 commit comments

Comments
 (0)