@@ -28,20 +28,19 @@ in your code:
28282 . [ lazy_static::lazy_static] ( https://docs.rs/lazy_static/latest/lazy_static )
29293 . [ std::sync::Mutex] ( https://doc.rust-lang.org/std/sync/struct.Mutex.html )
3030
31- 💡 Starting with ** Rust 1.63** , ` Mutex::new ` is ` const ` , you can use global
32- static ` Mutex ` locks without needing lazy initialization. See the ` mutex.rs `
33- example below.
31+ In a general case, you can start with ` OnceCell ` like in the ` once.rs ` example
32+ (see below).
3433
35- ## ` safe .rs`
34+ ## ` local .rs`
3635
37- A pure safe way to implement Singleton in Rust is using NO global variables
36+ A safe way to implement Singleton in Rust is using NO global variables
3837at all and passing everything around through function arguments.
3938The oldest living variable is an object created at the start of the ` main() ` .
4039
4140### How to Run
4241
4342``` bash
44- cargo run --bin singleton-safe
43+ cargo run --bin singleton-local
4544```
4645
4746### Output
@@ -55,8 +54,9 @@ Final state: 1
5554This is a singleton implementation via ` lazy_static! ` .
5655
5756` lazy-static ` allows declaring a static variable with lazy initialization
58- at first access. It is actually implemented via ` unsafe ` with ` static mut `
59- manipulation, however, it keeps your code free of explicit ` unsafe ` blocks.
57+ at first access. A drawback of ` lazy_static! ` is that it doesn't allow
58+ initialization at the arbitrary code place, only in the static block
59+ with predefined instructions.
6060
6161### How to Run
6262
@@ -73,10 +73,13 @@ Called 3
7373## ` once.rs `
7474
7575` OnceCell ` allows having a custom initialization of a singleton at an
76- ** arbitrary place** unlike ` lazy_static! ` , where the initialization must be
76+ ** arbitrary place** , unlike ` lazy_static! ` , where the initialization must be
7777placed in a static block. ` Mutex ` is still needed there to make an actual object
7878modifiable without an ` unsafe ` block.
7979
80+ A [ ` logger ` ] ( ../logger/ ) , a practical example of Singleton, is
81+ implemented via ` OnceCell ` .
82+
8083### How to Run
8184
8285``` bash
0 commit comments