I need to have a lot of data stored as an immutable variable that can be accessed by every function.
The optimal way would be to use a const DATA: [u64; 1000] = [ 1, 110101, ... ]
and that is possible because I know how this data looks like. Due to the huge amount of data that needs to be created, the only practical way is to initialize the constant with a function. That does not currently work in Rust, and it maybe will never, because I need to run loops in the initializer function which isn't even supported in the nightly build.
It should be possible to initialize the data in the main
function as ordinary let DATA: [u64; 1000] = ini_fn();
. Then I would need to give all subfunctions a reference to this data. That's kinda inconvenient; if a function foo3()
were to need the DATA
but gets called by foo2()
which gets called by foo1()
which gets called by main(), I would need to borrow the DATA
three times and really using it just once.
I am searching a more reasonable solution for this problem / some equivalent to this C code:
std::array<int, 64> ini_data()
{
std::array<int, 64> ret;
for(int i = 0; i < 64; i++)
{
ret[i] = i;
}
return ret;
}
const std::array<int, 64> DATA = ini_data();
void main()
{
print(DATA);
}
It would be better to have this data on the stack because speed is important, but I hope that the data gets cached so this is not a priority.