[フレーム]
BT

InfoQ Software Architects' Newsletter

A monthly overview of things you need to know as an architect or aspiring architect.

View an example

We protect your privacy.

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Unlock the full InfoQ experience

Unlock the full InfoQ experience by logging in! Stay updated with your favorite authors and topics, engage with content, and download exclusive resources.

Log In
or

Don't have an InfoQ account?

Register
  • Stay updated on topics and peers that matter to youReceive instant alerts on the latest insights and trends.
  • Quickly access free resources for continuous learningMinibooks, videos with transcripts, and training materials.
  • Save articles and read at anytimeBookmark articles to read whenever youre ready.

Topics

Choose your language

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+ global
senior 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.

About the Author

Sergio De Simone

Show moreShow less

Rate this Article

Adoption
Style

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

We protect your privacy.

BT

AltStyle によって変換されたページ (->オリジナル) /