In my Rust project, I need a globally hold, static array or vec that is initialized once where modules can register values or functions on. I thought, this would be possible using the lazy_static!-crate, but it doesn't seem so.
This is what I want to achieve:
- Module
a
initializes an array/vec with some data. - Module
b
(or further modules) then extend this array/vec to further data. - All this should only be done once at program startup, and the array won't be modified during the program's execution. It is just a lookup-table, globally hold, but once created from several modules.
This is my first draft, which does not work playground link
mod a
{
use lazy_static::lazy_static; // 1.4.0
lazy_static!{
#[derive(Debug)]
pub static ref TEST: Vec<u32> = vec![1, 2, 3];
}
}
mod b // when module b is removed, it works.
{
use lazy_static::lazy_static; // 1.4.0
use crate::a::TEST;
lazy_static!{
TEST.extend(vec![4, 5, 6]);
}
}
use a::TEST;
fn main() {
for i in 0..TEST.len() {
println!("{}", TEST[i]);
}
}
Can anybody help?