It looks like a use case for std::sync::Once
:
use std::sync::{Once, ONCE_INIT};
static INIT: Once = ONCE_INIT;
Then in your tests call
INIT.doit(|| unsafe { init(); });
Once
guarantees that your init
will only be executed once, no matter how many times you call INIT.doit()
.
More Related Contents:
- What are non-lexical lifetimes?
- What are Rust’s exact auto-dereferencing rules?
- Why can impl trait not be used to return multiple / conditional types?
- Passing mutable self reference to method of owned object
- Rust proper error handling (auto convert from one error type to another with question mark)
- How to create a static string at compile time
- How to call a method when a trait and struct use the same method name?
- How to convert ‘struct’ to ‘&[u8]’?
- How does the mechanism behind the creation of boxed traits work?
- Why does passing a closure to function which accepts a function pointer not work?
- Adding an append method to a singly linked list
- How do you set, clear and toggle a single bit in Rust?
- Lifetime of variables passed to a new thread
- How can I co-sort two Vecs based on the values in one of the Vecs?
- How can I distinguish between a deserialized field that is missing and one that is null?
- Cannot create a generic function that uses a literal zero
- How can I store a pattern in a variable in Rust?
- How to avoid temporary allocations when using a complex key for a HashMap?
- Why does compilation not fail when a member of a moved value is assigned to?
- Why does a mutable borrow of a closure through DerefMut not work?
- How to switch between Rust toolchains?
- What does “missing lifetime specifier” mean when storing a &str in a structure?
- Why can Serde not derive Deserialize for a struct containing only a &Path?
- How do I specify that a struct field must implement a trait?
- What does Rust’s unary || (parallel pipe) mean?
- How to create and write to memory mapped files?
- How does Rust’s type inference work across multiple statements?
- Downcast traits inside Rc for AST manipulation
- How to check in Rust if architecture is 32 or 64 bit?
- Is there any way to restrict a generic type to one of several types?