You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
panstromek opened this issue
May 4, 2024
· 2 comments
· Fixed by #125696
Labels
A-docsArea: documentation for any part of the project, including the compiler, standard library, and toolsT-libsRelevant to the library team, which will review and decide on the PR/issue.
Today I realized these are separate types. I used the experimental LazyLock before without realizing I can actually use stable OnceLock. These types are so similar that I never realized there are two of them.
Still, I don't think I quite understand the difference and the tradeoff (e.g. is some of them faster or not?). It would be useful if the docs pages linked to each other and explained the difference.
The text was updated successfully, but these errors were encountered:
panstromek
added
the
A-docs
Area: documentation for any part of the project, including the compiler, standard library, and tools
label
May 4, 2024
LazyLock is an API you can build on top of OnceLock, and is the "obvious one" to use in many cases. OnceLock permits more potential use-cases, being the more basic form, but also often is additional complication if you wanted LazyLock.
Rollup merge of rust-lang#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.
Fixesrust-lang#124696Fixesrust-lang#125615
A-docsArea: documentation for any part of the project, including the compiler, standard library, and toolsT-libsRelevant to the library team, which will review and decide on the PR/issue.
Location
https://doc.rust-lang.org/std/sync/struct.LazyLock.html
https://doc.rust-lang.org/std/sync/struct.OnceLock.html
https://doc.rust-lang.org/std/cell/struct.OnceCell.html
https://doc.rust-lang.org/std/cell/struct.LazyCell.html
Summary
Today I realized these are separate types. I used the experimental
LazyLock
before without realizing I can actually use stableOnceLock
. These types are so similar that I never realized there are two of them.Still, I don't think I quite understand the difference and the tradeoff (e.g. is some of them faster or not?). It would be useful if the docs pages linked to each other and explained the difference.
The text was updated successfully, but these errors were encountered: