Series: The Rust Annals

Vol. I Issue 58 nlopes.dev

Announcing Rust 1.57.0

Rust 1.57 brings panic! and assertions into const contexts, lets Cargo builds use custom profiles, and adds fallible capacity reservation to standard-library collections. Together, these changes make more invariants checkable at compile time, allow specialized build configurations without changing the regular release profile, and let collection users handle reservation failure instead of relying on the usual process abort.

Const evaluation can now panic and assert

Previously, panic! could not be used in const fn or other compile-time contexts. Rust 1.57 stabilizes it there and makes other standard-library APIs, including assert!, usable in const contexts.

This initial support does not include the full formatting infrastructure. A const panic! must use either a static string, as in panic!("..."), or one interpolated &str, as in panic!("{}", a). The latter must use {}, without format specifiers or other formatting traits. Support is expected to expand in the future.

Even with those constraints, compile-time assertions can directly verify invariants such as type sizes:

const _: () = assert!(std::mem::size_of::<u64>() == 8);
const _: () = assert!(std::mem::size_of::<u8>() == 1);

Custom Cargo profiles separate specialized builds

Cargo previously provided four profiles: dev, release, test, and bench. Rust 1.57 adds arbitrarily named profiles, allowing settings for a specialized build without applying them to ordinary release builds.

For example, a production profile can inherit release settings while enabling link-time optimization only for the final production build:

[profile.production]
inherits = "release"
lto = true

Every custom profile must name another profile from which it inherits default settings. Cargo commands that build code can select the profile with --profile production. Currently, its artifacts are placed in a separate directory—target/production in this example—so artifacts are not shared between profile directories.

Collections can reserve capacity without the usual allocator-failure abort

Rust 1.57 stabilizes fallible reservation APIs for Vec, String, HashMap, HashSet, and VecDeque. They let callers attempt to allocate backing storage and handle failure, instead of relying on Rust's usual behavior of aborting the process when the global allocator fails.

The result does not guarantee that the kernel has actually allocated the memory. For example, with Linux overcommit enabled, the memory might still be unavailable when code attempts to use it.

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