Series: The Rust Annals

Vol. I Issue 34 nlopes.dev

Announcing Rust 1.33.0

Expanded constant functions and the stabilization of pinning make this a substantial language and library release.

Rust 1.33’s two largest changes substantially expand what const fn bodies can express and stabilize pinning, which gives library authors a way to prevent pointees from moving and advances the work toward async/await. The release also permits imports under _, makes a broad set of standard-library operations available during constant evaluation, improves Cargo’s rebuild detection, and begins requiring verified email addresses for crates.io publishing.

Constant functions gain bindings, assignment, and unsafe calls

const fn bodies can now use several previously unavailable constructs:

  • irrefutable destructuring patterns, such as const fn foo((x, y): (u8, u8)) { ... };
  • let bindings, including mutable bindings such as let mut x = 1;;
  • assignment and assignment-operator expressions such as x = y and x += y, including projected targets such as struct fields and indexes like x[3] = 42;
  • expression statements such as 3;.

A const fn may also call a const unsafe fn inside an unsafe block:

const unsafe fn foo() -> i32 { 5 }
const fn bar() -> i32 {
    unsafe { foo() }
}

Together, these additions allow many more standard-library functions to be marked const.

Pinning fixes pointees in place, while imports can discard a name

This release introduces pinning through std::pin::Pin<P> and the Unpin marker trait. Some objects need a guaranteed memory location: moving a self-referential object would invalidate its pointers and could cause undefined behavior. A Pin<P> ensures that the pointee of pointer type P has a stable location, so it cannot be moved elsewhere or have its memory deallocated before it is dropped. The std::pin documentation describes the details.

Pinning will largely be used by library authors. Its stabilization is also a significant step toward async/await, though that feature is not complete; the remaining work was tracked at areweasyncyet.rs.

Items may now be imported as _. This brings a trait’s implementations into scope without putting its name in the namespace:

use std::io::Read as _;

// Allowed as there is only one `Read` in the module.
pub trait Read {}

More numeric, networking, collection, and time APIs become usable

Alongside the protected API inventory, the following methods are newly usable in const contexts: rotate_right and wrapping_{add, sub, mul, shl, shr} for all numeric types, and is_negative for all signed numeric types; count_zeros, leading_zeros, trailing_zeros, swap_bytes, from_be, from_le, to_be, and to_le for all numeric types. The protected get entry applies to all NonZero types, while the protected overflowing_{add, sub, mul, shl, shr}, rotate_left, and count_ones entries apply to all numeric types; is_positive applies to all signed numeric types.

The additional newly stable APIs omitted from that inventory are:

Cargo notices mid-build changes, and crates.io requires verified email

Cargo should now rebuild a crate if a file was modified during the initial build.

Starting at 2019-03-01 00:00 UTC, crates.io requires a verified email address for publishing. A user without one receives an error from cargo publish. Cargo had printed warnings during the previous release cycle; affected publishers should set and verify their address at crates.io/me. The requirement allows crates.io to comply with DMCA procedures. The address is never displayed publicly and is used only for crates.io operations; the change was announced in advance.

Reproduced from the Rust blog under its publication licence. Typeset in Literata.