std-type

Once: execute initialization once

Use Once to coordinate a one-time action. It does not store a typed result; OnceLock is usually clearer when initialization produces a value. A panicking initializer can poison Once. Expected stdout (not executed): initialized true Common mistake: Assuming Once automatically stores the closure return value.

Formal Syntax & Structure

rust syntaxrust
Once::new(); once.call_once(F)

Working Example

Example Snippetrust
fn main() {    use std::sync::Once;    let once = Once::new();    once.call_once(|| println!("initialized"));    once.call_once(|| println!("not called"));    println!("{}", once.is_completed());}
Rust Reference