Run code once in Rust.

Need to coordinate access to some resource in your tests? Need to run some initialization code required by multiple threads?

I’ve used the following code snippet a few times now. It blocks but it gets the job done. I’ve only used it for tests. I think I would only use it for initialization code in production.

use std::sync::{Once, ONCE_INIT};
static SYNC_OBJ: Once = ONCE_INIT;
pub fn do_some_init() {
SYNC_OBJ.call_once(|| {
let _ = some_init_fn();
});
}
A single golf clap? Or a long standing ovation?

By clapping more or less, you can signal to us which stories really stand out.