this post was submitted on 14 Jun 2023
2 points (100.0% liked)
Rust: Support
2 readers
1 users here now
Rules
-
Ask any question relevant to the Rust language and its ecosystem. No such questions shall ever be considered dumb!
-
Ask for contributions for your crate
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
First of all, thank you for this very elaborate answer. So that means OnceCell can never be used in a static but only as local variable / member variable? I see, that makes sense and also reduces its use cases by a lot of what I initially thought. But your example makes sense and the name/package also suggest it's not thread-safe in itself, I had just wrongly assumed OnceCell was meant to be used in static contexts.
Thanks!
Yeah, it doesn't help that in the
once_cell
crate, the thread-safe version was also calledOnceCell
; you had to choose betweenonce_cell::sync::OnceCell
andonce_cell::unsync::OnceCell
. But when it was added to the standard library,once_cell::sync::OnceCell
was renamed toOnceLock
to make them easier distinguishable. So