]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0015.md
Rollup merge of #107086 - clubby789:bootstrap-lock-pid-linux, r=albertlarsan68
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0015.md
1 A non-`const` function was called in a `const` context.
2
3 Erroneous code example:
4
5 ```compile_fail,E0015
6 fn create_some() -> Option<u8> {
7     Some(1)
8 }
9
10 // error: cannot call non-const fn `create_some` in constants
11 const FOO: Option<u8> = create_some();
12 ```
13
14 All functions used in a `const` context (constant or static expression) must
15 be marked `const`.
16
17 To fix this error, you can declare `create_some` as a constant function:
18
19 ```
20 // declared as a `const` function:
21 const fn create_some() -> Option<u8> {
22     Some(1)
23 }
24
25 const FOO: Option<u8> = create_some(); // no error!
26 ```