InfoQ Homepage News Rust 1.80 Adds Support for Lazy Statics, Extends Ranges in Patterns, and More
Rust 1.80 Adds Support for Lazy Statics, Extends Ranges in Patterns, and More
This item in japanese
Aug 06, 2024 2 min read
Write for InfoQ
Feed your curiosity. Help 550k+ globalsenior developers
each month stay ahead.Get in touch
Rust 1.80 stabilizes LazyCell and LazyLock, two new types that can be used to delay initialization of data until the first time they are accessed. It also brings support for exclusive ranges as well as a couple of related lint warnings. Additionally, it allows variadic functions without a named parameter for compatibility with C23, stabilizes many APIs, and more.
LazyCell and LazyLock enable lazy initialization of shared data, with LazyLock being thread-safe. Their companions OnceCell and OnceLock, which enable one-time initialization of shared data, were stabilized in Rust 1.70 and could be used for lazy initialization, too, but in a less ergonomic way.
This is how you can define a lazily-initialized global using LazyLock:
use std::sync::LazyLock;
static G_INT: LazyLock<u8> = LazyLock::new(|| 100);
fn main() {
let x = *G_INT; // initialization happens here
// ...
}
Compare that with OnceLock syntax, where you define a value without explicitly initializing it. Instead, you use the OnceLock::get_or_init() function the first time you access it:
use std::sync::OnceLock;
static G_INT: OnceLock<u8> = OnceLock::new();
fn main() {
let x = *G_INT.get_or_init(|| 100);
// ...
}
OnceLock and OnceCell serve a different purpose than their Lazy* counterparts, namely ensuring that a value is initialized only once. Using them for lazy initialization would require you to use the same initialization statement in every place where you access them, which is cumbersome.
Of the four types, LazyLock is the one that you can safely use in most contexts, with LazyCell being indicated if you want to remove any overhead related to concurrency and OnceLock and OnceCell being more flexible in terms of how you can handle initialization logic and supporting more complex use-cases.
Another useful addition to the language is support for exclusive ranges in pattern matching. Before version 1.80, Rust only supported inclusive endpoints, written as a..=b or ..=b. Now, you can also use a..b and ..b. With that, you can write:
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",
}
To remove the chance of off-by-one errors, Rust 1.80 introduced two new lints, non_contiguous_range_endpoints and overlapping_range_endpoints which will detect mistakes when adopting exclusive patterns in existing code.
Rust 1.80 introduces many more changes in the language, compiler, and standard library. One minor but notable new feature is support for variadic functions without a named parameter. This amounted to just removing a static check disallowing such functions, but makes the language a tad closer to C23, which supports that syntax.
For a detailed list of all new features and stabilizations in Rust 1.80, do not miss the official release notes.
This content is in the Rust topic
Related Topics:
-
Related Editorial
-
Related Sponsors
-
Popular across InfoQ
-
Grafana and GitLab Introduce Serverless CI/CD Observability Integration
-
TanStack Start: A New Meta Framework Powered by React or SolidJS
-
Redis Critical Remote Code Execution Vulnerability Discovered after 13 Years
-
Java News Roundup: OpenJDK JEPs for JDK 26, Spring RCs, Quarkus, JReleaser, Seed4J, Gradle
-
GitHub Expands Copilot Ecosystem with AgentHQ
-
If You Can’t Test It, Don’t Deploy It: The New Rule of AI Development?
-
Related Content
The InfoQ Newsletter
A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example