Rust 1.56.0 release and 2021 revision

The Rust team is pleased to announce a new version of the language – 1.56.0. With this release, the 2021 edition will also stabilize. Rust is a programming language that allows anyone to create reliable and efficient software.

If you installed a previous version of Rust using rustup, then to upgrade to version 1.56.0 you just need to run the following command:

rustup update stable

If you have not yet installed rustup, You can install it from the corresponding page of our website, as well as view detailed release notes on GitHub.

What’s stabilized at 1.56.0

Rust 2021

V May we wrote about the plans for the 2021 Rust revision. Revision is a mechanism for adding changes that could break backward compatibility. How this happens can be found in revision guide… This revision is small, especially compared to 2018, but there are a few more life-making changes in it. They require an update confirmation to avoid breaking in some special cases in already written code. For more information about the new functionality and how to migrate, we suggest that you yourself familiarize yourself with the new chapters of the editorial board:

Non-overlapping capture in closures

Closures automatically capture values ​​or identifier references that are used in the body of the closure, but until 2021 revision they always capture them entirely. The new non-overlapping capture function can make writing closures a lot easier, so let’s take a look at a small example:

// Код в 2015 или 2018 редакции
let a = SomeStruct::new();

// Убираем одно из полей структуры
drop(a.x);

// Ok: Используем другое поле структуры
println!("{}", a.y);

// Ошибка: До 2021 редакции потребуется полностью захватить `a`
let c = || println!("{}", a.y);
c();

To fix this, you would have to extract something like let y = &a.y; manually before closing to restrict its capture. Starting in Rust 2021, closures will automatically capture only the fields they use, so the above example will compile fine!

This behavior is only activated in a new revision because it can change the order in which fields are removed. As for all revision changes, automatic migration is available for them – it will update your closures for which it is important. Inside the closure will be inserted let _ = &a;to capture the entire structure as before.

Migration for 2021

The guide includes migration instructions for all new features and more generally for transition of an existing project to a new version. In many cases cargo fix can automate the necessary changes. You may even find that no changes to your code are needed for 2021!

As small as this edition may seem at first glance, it is still the result of the painstaking work of many contributors, which you can find in a separate tracker. Rust 2021 celebration and thanks!

Cargo rust-version

Cargo.toml now supports field [package] rust-versionwhich is used to specify the minimum supported Rust version for the crate. Cargo will exit with an early error if the condition is not met. This does not currently affect the dependency resolver, but the whole idea is to catch compatibility issues before they turn into cryptic compiler errors.

New bindings in binding @ pattern

Pattern matching in Rust can be written with a single identifier that binds the entire value. It is followed by @ and a more precise structural pattern, however this did not allow for additional bindings in this pattern – until now!

struct Matrix {
    data: Vec<f64>,
    row_len: usize,
}

// Раньше нам было необходимо разделить присваивание
// всей структуры и чтение её частей.
let matrix = get_matrix();
let row_len = matrix.row_len;
// или с помощью деструктурирования:
let Matrix { row_len, .. } = matrix;

// С Rust 1.56 можно сделать это в одно действие!
let matrix @ Matrix { row_len, .. } = get_matrix();

In fact, this was possible before Rust 1.0, but was removed due to the well-known at that time insolvency. With the development of tools for checking borrowings, through long and intensive compiler tests, I determined that it was, after all, safe, and included this feature in Rust!

Stabilized APIs

The following methods and trait implementations have been stabilized:

The following previously stabilized API steels const:

Other changes

Syntax, package manager Cargo and Clippy analyzer also underwent some changes.

Participants 1.56.0

Lots of people came together to create Rust 1.56.0 and the 2021 edition. We couldn’t have done this without all of you. Thanks!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *