This was surprising to me. Surely either slice indexing shouldn't be allowed or should be compile time evaluated? Or am I misunderstanding the const fn concept?
const fn is a function that can be evaluated at both compile time and runtime. To restrict it to run purely at compile time, assign its return value to constant like const FOO: u8 = foo(..);.
error: any use of this value will cause an error
--> src/main.rs:2:5
|
2 | a[4]
| ^^^^
| |
| index out of bounds: the len is 4 but the index is 4
| inside call to `foo` at src/main.rs:5:17
...
5 | const FOO: u8 = foo(&[0, 1, 2, 3]);
| -----------------------------------
|
= note: #[deny(const_err)] on by default
error: any use of this value will cause an error
--> src/main.rs:3:5
|
3 | a[4]
| ^^^^
| |
| index out of bounds: the len is 4 but the index is 4
| inside call to `foo` at src/main.rs:16:9
...
16 | / const_call!(
17 | | foo(&[0,1,2,3]) => u8 // array too small
18 | | );
| |______- in this macro invocation