The Rust team is happy to announce a new version of Rust, 1.56.0. This stabilizes the 2021 edition as well. Rust is a programming language empowering everyone to build reliable and efficient software.
If you have a previous version of Rust installed via rustup, getting Rust 1.56.0 is as easy as:
$ rustup update stable
If you don't have it already, you can get rustup
from the appropriate page on our website, and check out the
detailed release notes for 1.56.0 on GitHub.
What's in 1.56.0 stable
Rust 2021
We wrote about plans for the Rust 2021 Edition in May. Editions are a mechanism for opt-in changes that may otherwise pose backwards compatibility risk. See the edition guide for details on how this is achieved. This is a smaller edition, especially compared to 2018, but there are still some nice quality-of-life changes that require an edition opt-in to avoid breaking some corner cases in existing code. See the new chapters of the edition guide below for more details on each new feature and guidance for migration.
- Disjoint capture: closures now capture individual named fields rather than always capturing whole identifiers.
IntoIterator
for arrays:array.into_iter()
now iterates over items by value instead of by reference.- Or patterns in macro-rules now match top-level
A|B
in:pat
. - Default Cargo feature resolver is now version 2.
- Additions to the prelude:
TryInto
,TryFrom
, andFromIterator
are now in scope by default. - Panic macros now always expect format strings, just like
println!()
. - Reserving syntax for
ident#
,ident"..."
, andident'...'
. - Warnings promoted to errors:
bare_trait_objects
andellipsis_inclusive_range_patterns
.
Disjoint capture in closures
Closures automatically capture values or references to identifiers that are used in the body, but before 2021, they were always captured as a whole. The new disjoint-capture feature will likely simplify the way you write closures, so let's look at a quick example:
// 2015 or 2018 edition code
let a = SomeStruct::new();
// Move out of one field of the struct
drop(a.x);
// Ok: Still use another field of the struct
println!("{}", a.y);
// Error: Before 2021 edition, tries to capture all of `a`
let c = || println!("{}", a.y);
c();
To fix this, you would have had to extract something like let y = &a.y;
manually before the closure to limit its capture. Starting in Rust 2021,
closures will automatically capture only the fields that they use, so the
above example will compile fine!
This new behavior is only activated in the new edition, since it can change
the order in which fields are dropped. As for all edition changes, an
automatic migration is available, which will update your closures for which
this matters by inserting let _ = &a;
inside the closure to force the
entire struct to be captured as before.
Migrating to 2021
The guide includes migration instructions for all new features, and in general
transitioning an existing project to a new edition.
In many cases cargo fix
can automate the necessary changes. You may even
find that no changes in your code are needed at all for 2021!
However small this edition appears on the surface, it's still the product of a lot of hard work from many contributors: see our dedicated celebration and thanks tracker!
rust-version
Cargo Cargo.toml
now supports a [package]
rust-version
field to specify
the minimum supported Rust version for a crate, and Cargo will exit with an
early error if that is not satisfied. This doesn't currently influence the
dependency resolver, but the idea is to catch compatibility problems before
they turn into cryptic compiler errors.
binding @ pattern
New bindings in Rust pattern matching can be written with a single identifier that binds
the entire value, followed by @
and a more refined structural pattern,
but this has not allowed additional bindings in that pattern -- until now!
struct Matrix {
data: Vec<f64>,
row_len: usize,
}
// Before, we need separate statements to bind
// the whole struct and also read its parts.
let matrix = get_matrix();
let row_len = matrix.row_len;
// or with a destructuring pattern:
let Matrix { row_len, .. } = matrix;
// Rust 1.56 now lets you bind both at once!
let matrix @ Matrix { row_len, .. } = get_matrix();
This actually was allowed in the days before Rust 1.0, but that was removed due to known unsoundness at the time. With the evolution of the borrow checker since that time, and with heavy testing, the compiler team determined that this was safe to finally allow in stable Rust!
Stabilized APIs
The following methods and trait implementations were stabilized.
std::os::unix::fs::chroot
UnsafeCell::raw_get
BufWriter::into_parts
core::panic::{UnwindSafe, RefUnwindSafe, AssertUnwindSafe}
(previously only instd
)Vec::shrink_to
String::shrink_to
OsString::shrink_to
PathBuf::shrink_to
BinaryHeap::shrink_to
VecDeque::shrink_to
HashMap::shrink_to
HashSet::shrink_to
The following previously stable functions are now const
.
Other changes
There are other changes in the Rust 1.56.0 release: check out what changed in Rust, Cargo, and Clippy.
Contributors to 1.56.0
Many people came together to create Rust 1.56.0 and the 2021 edition. We couldn't have done it without all of you. Thanks!