【Rust】Subモジュールで定義したMutexの使い方

mutexの場合でも、基本的にはstaticと同じようにモジュール名から呼び出せば使えるようになる。

sub file

use once_cell::sync::Lazy;
use std::sync::Mutex;

pub static BOX: Lazy<Mutex<Vec<String>>> = Lazy::new(|| Mutex::new(vec![]));

main

mod sub1;

fn main() {
    sub1::BOX.lock().unwrap().push("text1".to_string());
    println!("{:?}", sub1::BOX);
}

Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.67s
Running `target/debug/app`
Lazy { cell: OnceCell(Mutex { data: [“text1”], poisoned: false, .. }), init: “..” }

なるほど、そういうことか…