summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_error_codes/src/error_codes/E0015.md
blob: 021a0219d13e2b60b8e79b49f3fb37132dc6789b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
A constant item was initialized with something that is not a constant
expression.

Erroneous code example:

```compile_fail,E0015
fn create_some() -> Option<u8> {
    Some(1)
}

const FOO: Option<u8> = create_some(); // error!
```

The only functions that can be called in static or constant expressions are
`const` functions, and struct/enum constructors.

To fix this error, you can declare `create_some` as a constant function:

```
const fn create_some() -> Option<u8> { // declared as a const function
    Some(1)
}

const FOO: Option<u8> = create_some(); // ok!

// These are also working:
struct Bar {
    x: u8,
}

const OTHER_FOO: Option<u8> = Some(1);
const BAR: Bar = Bar {x: 1};
```