I would like to allocate an SVector<f64, 7>
as a static variable. It seems that nalgebra's library-provided builders cannot do this, as none of them are const
. Is there a workaround?
Here's what I tried (this doesn't compile):
const C5: SVector<f64, 7> = SVector::<f64, 7>::from_row_slice(&[
5179.0 / 57600.0,
0.0,
7571.0 / 16695.0,
393.0 / 640.0,
-92097.0 / 339200.0,
187.0 / 2100.0,
1.0 / 40.0,
]);
Edit: As @kmdreko pointed out, the following questions were more appropriate for a separate post:
Why can't Rust use non-const
-qualified functions to allocate const
variables? Why does it matter whether or not a function is const
if the data it's operating on is const
?
I found a const constructor: from_array_storage
.
use nalgebra::{ArrayStorage, SVector};
const C5: SVector<f64, 7> = SVector::<f64, 7>::from_array_storage(ArrayStorage([[
5179.0 / 57600.0,
0.0,
7571.0 / 16695.0,
393.0 / 640.0,
-92097.0 / 339200.0,
187.0 / 2100.0,
1.0 / 40.0,
]]));
For your other questions:
const
and static
values. If you need a global value that can't be constructed at compile time, then you can use a static
, usually with something like Lazy
(LazyLock
in future Rust standard library), or thread_local
.let
and static
bindings at compile time if it is able and decides it is beneficial for optimization.