May 12, 2021

What to expect in Rust 2021

What to expect in Rust 2021

What to expect in Rust 2021

The Rust team has revealed new insight into what to expect from Rust 2021, which is scheduled to be released in October 2021. 

Rust 2021 is the third edition of the language. Editions in Rust are opt-in updates that introduce features that are backwards incompatible. Since they are opt-in, developers won’t see the changes unless they decide to migrate to that edition. 

According to the team, crates in one edition are interoperable with crates compiled in a different edition. “This ensures that the decision to migrate to a newer edition is a ‘private one’ that the crate can make without affecting others,” the Rust team wrote in a blog post

RELATED CONTENT: What’s all the fuss about Rust?

One change being made in this edition is a new prelude, which is a module that contains the things imported automatically in every module. Adding a trait to the prelude can break existing code, but as a workaround, Rust 2021 will include a new prelude that has three additions over the current one: std::convert::TryInto, std::convert::TryFrom, and std::iter::FromIterator. 

This edition will also introduce a small workaround to IntoIterator, which currently causes breaks to existing code. “In Rust 2015 and 2018 code, the compiler will still resolve array.into_iter() to (&array).into_iter() like before, as if the trait implementation does not exist. This only applies to the .into_iter() method call syntax. It does not affect any other syntax such as for e in [1, 2, 3], iter.zip([1, 2, 3]) or IntoIterator::into_iter([1, 2, 3]). Those will start to work in all editions. While it’s a shame that this required a small hack to avoid breakage, we’re very happy with how this solution keeps the difference between the editions to an absolute minimum. Since the hack is only present in the older editions, there is no added complexity in the new edition,” the Rust team explained.

Rust 2021 will also introduce a new feature resolver that no longer merges all requested features in crates that are dependencies. 

In the new edition, closures, which automatically capture whatever is referred to from within their body, will only capture the fields they actually use. 

Rust 2021 will introduce a more consistent panic!() macro as well. The new macro won’t accept arbitrary expressions as their only argument. 

This release will also reserve some syntax to make space for new syntax down the line. It will reserve prefix#identifier, prefix”string”, prefix’c’, and prefix#123. 

In addition, it will make two existing lints hard errors instead of just warnings as they are currently. The use of the keyword dyn to identify trait objects will be mandatory, and the deprecated … syntax for inclusive range patterns won’t be accepted anymore. 

Finally, in Rust 1.53, patterns were extended to support | nested anywhere in the pattern, which enabled developers to write Some(1 | 2) instead of Some(1) | Some(2). This change affects macro_rules macros because they can accepted patterns using the :pat specifier, but :pat doesn’t match |. Starting in Rust 2021, the :pat fragment specifier will be able to match A | B. 

These features are the final features that will be available in Rust 2021. They were decided upon by the Rust 2021 Working Group by meeting two criteria: 1) they had to be approved by the appropriate Rust team, and 2) their implementation had to have been far enough along that the group was confident in their ability to be completed in time for the planned milestones. 

The Rust 2021 Working Group plans to have these changes merged and tested by September in order to ensure Rust 2021 can be added to Rust 1.56.0. Rust 1.56.0 will be in beta for six weeks and released as stable on October 21st. It will release another announcement on the new edition in July, at which point all changes should be implemented and ready for testing.