Series: The Rust Annals
Vol. I Issue 84 nlopes.dev
Announcing Rust 1.83.0
Incremental minor release featuring extensive low-level utility stabilizations and const evaluation improvements. Lacks breaking changes, major ecosystem shifts, or historic milestone status.
New const capabilities
This release includes several large extensions to what code running in const contexts can do. This refers to all code that the compiler has to evaluate at compile-time: the initial value of const and static items, array lengths, enum discriminant values, const generic arguments, and functions callable from such contexts (const fn).
References to statics.
So far, const contexts except for the initializer expression of a static item were forbidden from referencing static items.
This limitation has now been lifted:
static S: i32 = 25;
const C: &i32 = &S;
Note, however, that reading the value of a mutable or interior mutable static is still not permitted in const contexts. Furthermore, the final value of a constant may not reference any mutable or interior mutable statics:
static mut S: i32 = 0;
const C1: i32 = unsafe { S };
// error: constant accesses mutable global memory
const C2: &i32 = unsafe { &S };
// error: encountered reference to mutable memory in `const`
These limitations ensure that constants are still “constant”: the value they evaluate to, and their meaning as a pattern (which can involve dereferencing references), will be the same throughout the entire program execution.
That said, a constant is permitted to evaluate to a raw pointer that points to a mutable or interior mutable static:
static mut S: i32 = 64;
const C: *mut i32 = &raw mut S;
Mutable references and pointers. It is now possible to use mutable references in const contexts:
const fn inc(x: &mut i32) {
*x += 1;
}
const C: i32 = {
let mut c = 41;
inc(&mut c);
c
};
Mutable raw pointers and interior mutability are also supported:
use std::cell::UnsafeCell;
const C: i32 = {
let c = UnsafeCell::new(41);
unsafe { *c.get() += 1 };
c.into_inner()
};
However, mutable references and pointers can only be used inside the computation of a constant, they cannot become a part of the final value of the constant:
const C: &mut i32 = &mut 4;
// error[E0764]: mutable references are not allowed in the final value of constants
This release also ships with a whole bag of new functions that are now stable in const contexts (see the end of the “Stabilized APIs” section).
These new capabilities and stabilized APIs unblock an entire new category of code to be executed inside const contexts, and we are excited to see how the Rust ecosystem will make use of this!