You can wrap the Vec in a Mutex
Rust
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
Maybe lazy_static? Personally I'd just pass a borrow to the vec around. It's very common for programs to have some global state that they pass around to different parts.
🤔 I thought lazy_static was deprecated in favor of one_cell
Ah I didn't realize most people have moved onto OnceCell. The issue with both lazy static and oncecell is that they can only be assigned to once. You need a global mutable state, so neither OnceCell or lazy_static are the right choice.
You're going to be fighting the borrow checker if you try to have global mutable state. It will bite you eventually. You can potentially use an interior mutablity pattern along with a mutex. Have you looked into interior mutability?
Can't you just use the get_or_init
method instead of get
inside the push_log
method? This would initialize the cell on first use. You'd still need a Mutex inside of it to acquire a mutable reference to the vector.