包阅导读总结
1.
关键词:Rust 1.80、Lazy Statics、Ranges、Patterns、Stabilizations
2.
总结:Rust 1.80 带来诸多新特性,包括支持 LazyCell 和 LazyLock 实现延迟初始化、扩展模式中的范围、引入新的 lint 警告、支持无命名参数的可变参数函数以及稳定许多 API 等。
3.
主要内容:
– Rust 1.80
– 稳定 LazyCell 和 LazyLock,用于延迟初始化数据,直到首次访问,LazyLock 是线程安全的。
– 支持独占范围,引入相关 lint 警告以避免错误。
– 为兼容 C23 支持无命名参数的可变参数函数,稳定许多 API 。
– 对比了 OnceCell 和 OnceLock 与 LazyCell 和 LazyLock 的差异。
– 详细新特性和稳定内容见官方发布说明。
思维导图:
文章来源:infoq.com
作者:Sergio De Simone
发布时间:2024/8/6 0:00
语言:英文
总字数:445字
预计阅读时间:2分钟
评分:83分
标签:Rust,延迟初始化,模式匹配,并发性,C23 兼容性
以下为原文内容
本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com
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.