]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0015.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0015.md
1 A constant item was initialized with something that is not a constant
2 expression.
3
4 Erroneous code example:
5
6 ```compile_fail,E0015
7 fn create_some() -> Option<u8> {
8     Some(1)
9 }
10
11 const FOO: Option<u8> = create_some(); // error!
12 ```
13
14 The only functions that can be called in static or constant expressions are
15 `const` functions, and struct/enum constructors.
16
17 To fix this error, you can declare `create_some` as a constant function:
18
19 ```
20 const fn create_some() -> Option<u8> { // declared as a const function
21     Some(1)
22 }
23
24 const FOO: Option<u8> = create_some(); // ok!
25
26 // These are also working:
27 struct Bar {
28     x: u8,
29 }
30
31 const OTHER_FOO: Option<u8> = Some(1);
32 const BAR: Bar = Bar {x: 1};
33 ```