rust/library/core
Guillaume Gomez ee04e0f35e
Rollup merge of #125696 - workingjubilee:please-dont-say-you-are-lazy, r=Nilstrieb
Explain differences between `{Once,Lazy}{Cell,Lock}` types

The question of "which once-ish cell-ish type should I use?" has been raised multiple times, and is especially important now that we have stabilized the `LazyCell` and `LazyLock` types. The answer for the `Lazy*` types is that you would be better off using them if you want to use what is by far the most common pattern: initialize it with a single nullary function that you would call at every `get_or_init` site. For everything else there's the `Once*` types.

"For everything else" is a somewhat weak motivation, as it only describes by negation. While contrasting them is inevitable, I feel positive motivations are more understandable. For this, I now offer a distinct example that helps explain why `OnceLock` can be useful, despite `LazyLock` existing: you can do some cool stuff with it that `LazyLock` simply can't support due to its mere definition.

The pair of `std::sync::*Lock`s are usable inside a `static`, and can serve roles in async or multithreaded (or asynchronously multithreaded) programs that `*Cell`s cannot. Because of this, they received most of my attention.

Fixes #124696
Fixes #125615
2024-06-04 21:41:34 +02:00
..
benches Write char::DebugEscape sequences using write_str 2024-05-20 10:04:44 +02:00
src Rollup merge of #125696 - workingjubilee:please-dont-say-you-are-lazy, r=Nilstrieb 2024-06-04 21:41:34 +02:00
tests Add function core::iter::chain 2024-06-04 10:51:05 +02:00
Cargo.toml Remove now outdated comment since we bumped stage0 2024-05-24 08:08:41 +02:00