What’s New in Rust 1.80? Lazy Initialization, cfg Checks, and Exclusive Ranges
Rust 1.80 introduces LazyCell and LazyLock for deferred data initialization, adds cfg name and value checking to catch configuration errors, supports exclusive range patterns for more concise matching, and stabilizes APIs such as Rc, Arc, Duration, Option, Seek, BinaryHeap, and NonNull, enhancing safety and ergonomics for developers.
Introduction
Rust 1.80 was officially released on July 25, 2024, bringing new lazy types that defer data initialization until the first access.
LazyCell and LazyLock
LazyCell and LazyLock are new types that delay data initialization. LazyLock is thread‑safe and suitable for static values, while LazyCell is not thread‑safe but can be used in thread‑local statics.
use std::sync::LazyLock;
use std::time::Instant;
static LAZY_TIME: LazyLock<Instant> = LazyLock::new(Instant::now);
fn main() {
let start = Instant::now();
std::thread::scope(|s| {
s.spawn(|| {
println!("Thread lazy time is {:?}", LAZY_TIME.duration_since(start));
});
println!("Main lazy time is {:?}", LAZY_TIME.duration_since(start));
});
}cfg Name and Value Checking
Cargo 1.80 now includes checks for cfg names and values, helping catch misspellings and incorrect configurations.
fn main() {
println!("Hello, world!");
#[cfg(feature = "crayon")]
rayon::join(
|| println!("Hello, Thing One!"),
|| println!("Hello, Thing Two!"),
);
}The compiler emits a warning like:
warning: unexpected `cfg` condition value: `crayon`
--> src/main.rs:4:11
|
4 | #[cfg(feature = "crayon")]
| ^^^^^^^^^^--------
| |
| help: there is an expected value with a similar name: "rayon"
= note: expected values for `feature` are: `rayon`
= help: consider adding `crayon` as a feature in `Cargo.toml`Exclusive Range Patterns
Rust 1.80 now supports exclusive range patterns (a..b), simplifying pattern matching and reducing the need for separate constants for the upper bound.
pub fn size_prefix(n: u32) -> &'static str {
const K: u32 = 10u32.pow(3);
const M: u32 = 10u32.pow(6);
const G: u32 = 10u32.pow(9);
match n {
..K => "",
K..M => "k",
M..G => "M",
G.. => "G",
}
}More Stable APIs
Rust 1.80 also stabilizes many APIs, including implementations for Rc and Arc , and enhancements to Duration , Option , Seek , BinaryHeap , and NonNull .
Updating to Rust 1.80
Developers using older toolchains can update to the latest stable version with:
$ rustup update stable21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
