Rust 1.56.0 arrives, delivering Rust 2021 Edition support

Rust 1.56.0 arrives, delivering Rust 2021 Edition support

The team behind programming language Rust has pushed out version 1.56.0 — the first release to support the Rust 2021 Edition, which is now considered stable.

Rust 2021 is effectively the third major Edition of the Rust language, and introduces some changes that may cause compatibility issues with older code — unlike the more frequent version releases that aim to preserve stability for developers. Editions are the Rust language’s way of allowing changes that may break compatibility by letting developers of crates — packaged code — specify which Edition each crate supports.  

According to the Rust team, the Rust 2021 Edition introduces fewer changes than Rust 2018, but describes these as “nice quality-of-life changes” that require an Edition opt-in to avoid breaking some corner cases in existing code. Plans for the Rust 2021 Edition were outlined back in a blog post in May.

Among the changes introduced is disjoint capture in closures, which makes captures more precise than before. In Rust 2018 and earlier, closures would capture entire variables, such as an entire structure, even if the closure only uses one field. This new behaviour is only activated in the new Edition, since it can change the order in which fields are dropped.

Rust 2021 also introduces changes to the Panic macro for greater consistency. The new panic!() macro will no longer accept arbitrary expressions as the only argument. Instead, it will always process the first argument as a format string, just like println!().

Another change sees new additions to the prelude which includes TryInto, TryFrom and FromIterator from the Rust standard library. The Rust team warns that this might make calls to trait methods ambiguous which could make some code fail to compile, but a migration lint has also been added in Rust 2021 that tries to detect cases where functions or methods are called that have the same name as the methods defined in one of the new prelude traits.

Starting in Rust 2021, the new feature resolver for the Cargo package manager is the default option. This implements a different approach to resolving dependencies to the previous default behaviour, which was to merge features for a package that is referred to multiple times in the dependency graph. Cargo also supports a rust-version field to specify the minimum supported Rust version for a crate, and Cargo will exit with an error if that is not satisfied.

Also in Rust 2021, new bindings are supported for binding @ patterns. This allows for additional bindings in a pattern, where pattern matching is written with a single identifier that binds the entire value.

The Rust Edition Guide contains includes migration instructions for all new features, and guidance on transitioning an existing project to a new Edition in general. In many cases cargo fix can automate any necessary changes, according to the Rust team.

Detailed release notes for Rust 1.56.0 are available on GitHub.