# Assigning a fn to a const variable

**URL:** <https://internals.rust-lang.org/t/assigning-a-fn-to-a-const-variable/9363>\
**Category:** language design\
**Created:** [February 5, 2019, 7:36am UTC](https://internals.rust-lang.org/t/assigning-a-fn-to-a-const-variable/9363 "2019-02-05T07:36:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eshikafe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/eshikafe/32/5171_2.png) [@eshikafe](https://internals.rust-lang.org/u/eshikafe)\
**Post date:** [February 5, 2019, 7:36am UTC](https://internals.rust-lang.org/t/assigning-a-fn-to-a-const-variable/9363/1 "2019-02-05T07:36:24Z")

</div>

The following constants are required globally in my code:

```rust
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

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

fn main() {

   println!("{}", A);
}

```

```rust
error[E0015]: calls in constants are limited to tuple structs and tuple variants
–&gt; 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`
–&gt; src\main.rs:3:20

```

---

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://internals.rust-lang.org/u/jethrogb)\
**Post date:** [February 5, 2019, 10:16am UTC](https://internals.rust-lang.org/t/assigning-a-fn-to-a-const-variable/9363/2 "2019-02-05T10:16:36Z")

</div>

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](https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md) for more details on the design of `const fn`s.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [May 6, 2019, 10:16am UTC](https://internals.rust-lang.org/t/assigning-a-fn-to-a-const-variable/9363/3 "2019-05-06T10:16:41Z")

</div>

Closed on May 6, 2019, 10:16am UTC

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