I am trying to initialize a static variable at runtime using lazy_static crate. But I'm getting no rules expected the token E1 error while compiling. This is the link lazy_static I followed
use lazy_static::lazy_static;
lazy_static! {
static E1: f64 = (1.0 - f64::sqrt(1.5)) / (1.0 + f64::sqrt(0.5));
}
fn main() {
println!("{}", E1);
}
You forgot the
reftoken afterstatic. This is just some custom grammar oflazy_staticto express that this static works a bit different is only accessible by reference.That's also the reason you need to dereference the value as the static variable is an opaque type. In many contexts this is not required as the type implements
Deref<Target = f64>. But here it is.Also consider using
once_cellwhich lets you achieve the same thing but without macro.