Assigning a fn to a const variable

The following constants are required globally in my code:

const A: f64 = 1.0/(2.0_f64.sqrt());
const B: f64 = 1.0/10.0_f64.sqrt();
const C: f64 = 1.0/42.0_f64.sqrt();

Rust should support this by default. It is not efficient to hard code the sqrt of a number before it can be assigned to a const variable.

The following sample code throws an error

const A: f64 = 1.0/4.0_f64.sqrt();

fn main() {

   println!("{}", A);
}
error[E0015]: calls in constants are limited to tuple structs and tuple variants
–> src\main.rs:3:20
|
3 | const A: f64 = 1.0/4.0_f64.sqrt();
| ^^^^^^^^^^^^^^
|
note: a limited form of compile-time function evaluation is available on a nightly compiler via  `const fn`
–> src\main.rs:3:20

This is not a question and support for this is being considered as part of the const fn effort (as mentioned in the error message). See RFC 911 for more details on the design of const fns.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.