Series: The Rust Annals
Vol. I Issue 57 nlopes.dev
Announcing Rust 1.56.0 and Rust 2021
Rust 1.56 stabilizes the Rust 2021 edition, an edition-scale set of opt-in language and tooling changes.
Rust 1.56.0 stabilizes the Rust 2021 edition, whose opt-in changes include more precise closure capture, by-value array iteration, a new Cargo feature resolver default, and several consistency and prelude improvements. The release also permits nested bindings in binding @ pattern, lets crates declare their minimum supported Rust version, and expands the stable and const standard-library surface.
Rust 2021 makes targeted, opt-in compatibility changes
Editions permit opt-in changes that may otherwise pose backwards-compatibility risks. Rust 2021 is smaller than Rust 2018, but introduces several quality-of-life changes that require an edition boundary to avoid breaking corner cases in existing code:
- Disjoint capture: closures capture individual named fields rather than always capturing whole identifiers.
IntoIteratorfor arrays:array.into_iter()iterates over items by value instead of by reference.- Or patterns in macro rules:
:patmatches top-levelA|B. - The default Cargo feature resolver is version 2.
- Prelude additions:
TryInto,TryFrom, andFromIteratorare in scope by default. - Panic macros always expect format strings, like
println!(). - Syntax is reserved for
ident#,ident"...", andident'...'. - Two warnings become errors:
bare_trait_objectsandellipsis_inclusive_range_patterns.
Before Rust 2021, closures captured identifiers as a whole. Consequently, this 2015 or 2018 edition example tries to capture all of a, even though the closure only uses a.y:
// 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();Previously, limiting the capture required manually extracting a reference such as let y = &a.y; before the closure. In Rust 2021, the closure automatically captures only the field it uses, so the example compiles. This behavior is edition-gated because it can change the order in which fields are dropped. Automatic migration inserts let _ = &a; inside affected closures to preserve whole-struct capture where that matters.
The edition migration guide covers the changes. In many cases, cargo fix can automate the necessary edits, and some projects may need no code changes.
Patterns can bind a whole value and its parts at once
A pattern can bind an entire value with an identifier followed by @ and a refined structural pattern. Rust 1.56 allows that structural pattern to introduce additional bindings, avoiding separate statements:
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 form existed before Rust 1.0 but was removed because of known unsoundness. Following the borrow checker's evolution and heavy testing, the compiler team determined that it was safe to allow on stable Rust.
Cargo can reject an unsupported compiler version early
Cargo.toml now supports a [package] rust-version field declaring a crate's minimum supported Rust version. Cargo exits with an early error when that requirement is not satisfied, helping expose compatibility problems before they become cryptic compiler errors. The field does not currently influence dependency resolution.
Standard-library stabilization includes capacity control and more const functions
The stable API set adds Unix chroot, raw access to UnsafeCell, recovery of a BufWriter's parts, and shrink_to operations across the listed collection and owned-buffer types. core::panic::{UnwindSafe, RefUnwindSafe, AssertUnwindSafe}, previously available only in std, is also stabilized.
The listed std::mem::transmute and slice operations were already stable and are now also usable in const contexts.