]> git.proxmox.com Git - rustc.git/blame - src/librustc_error_codes/error_codes/E0015.md
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0015.md
CommitLineData
60c5eb7d
XL
1A constant item was initialized with something that is not a constant
2expression.
3
4Erroneous code example:
5
6```compile_fail,E0015
7fn create_some() -> Option<u8> {
8 Some(1)
9}
10
11const FOO: Option<u8> = create_some(); // error!
12```
13
14The only functions that can be called in static or constant expressions are
15`const` functions, and struct/enum constructors.
16
17To fix this error, you can declare `create_some` as a constant function:
18
19```
20const fn create_some() -> Option<u8> { // declared as a const function
21 Some(1)
22}
23
24const FOO: Option<u8> = create_some(); // ok!
25
26// These are also working:
27struct Bar {
28 x: u8,
29}
30
31const OTHER_FOO: Option<u8> = Some(1);
32const BAR: Bar = Bar {x: 1};
33```