once
import std::sync::once; · source
Types
Once
type struct Once {
inner: OnceInner*;
static new() -> Once;
call_once(&this, init: () -> void) -> void;
}
Run an initializer exactly once across all threads. The first caller runs it; every other caller blocks until it finishes, and thereafter call_once returns immediately.
import std::sync::once;
mut init: Once = Once::new();
init.call_once(() -> void { setup_tables(); });
The initializer is a () -> void function pointer, and a non-capturing lambda is one.
The backing pthread_once_t starts all-zero, which is PTHREAD_ONCE_INIT, so no separate init call is needed.
Properties
| Property | Type |
|---|---|
inner | OnceInner* |
Constructors
static new() -> Once;
static new() -> Once;Construct a fresh Once in the not-yet-run state. Panics on allocation failure (Once has no fallible constructor - the expected use is in a module-level constant, where allocation happens once and panic-on-failure is the right default).
Methods
call_once(&this, init: () -> void) -> void;
call_once(&this, init: () -> void) -> void;Run init exactly once across all threads that call call_once on this Once. Threads calling concurrently block until the initializer completes.
Trait implementations
implement trait Drop for struct Once
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Once {
inner: OnceInner*;
}Once: one-shot initialization synchronization.
Once.inner: OnceInner*type struct OnceInner {
pthread_buf: [u8;
4];
}static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Once::new() -> OnceConstruct a fresh Once in the not-yet-run state. Panics on allocation failure (Once has no fallible constructor - the expected use is in a module-level constant, where allocation happens once and panic-on-failure is the right default).
type struct Once {
inner: OnceInner*;
}Once: one-shot initialization synchronization.
std::sync::onceOnce::call_once(&this, init: () -> void) -> voidRun init exactly once across all threads that call call_once on this Once. Threads calling concurrently block until the initializer completes.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) init: () -> voidimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut init: Oncestatic Once::new() -> OnceConstruct a fresh Once in the not-yet-run state. Panics on allocation failure (Once has no fallible constructor - the expected use is in a module-level constant, where allocation happens once and panic-on-failure is the right default).
mut init: OnceOnce::call_once(&this, init: () -> void) -> voidRun init exactly once across all threads that call call_once on this Once. Threads calling concurrently block until the initializer completes.
Once.inner: OnceInner*implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference