Rust does some constant propagation itself, but I don't think it would ever move a runtime panic to a compile time error, at least not implicitly. LLVM will do much more constant propagation while optimizing, but still preserving the runtime semantics as if it had run normally. In fact your example compiles down to nothing but the panic!
Note that this means if your constant is const, you'll get the unconditional_panic error.
fn main() {
let array = [3; 5];
const INDEX: usize = 7;
let value = array[INDEX];
println!("The value of the array at index {} is {}", INDEX, value);
}
error: this operation will panic at runtime
--> src/main.rs:4:17
|
4 | let value = array[INDEX];
| ^^^^^^^^^^^^ index out of bounds: the length is 5 but the index is 7
|
= note: `#[deny(unconditional_panic)]` on by default
Yes, in Rust const variables are guarantied to be evaluated a compilation time. so they need to be initialized with an expression using only const values and functions.
Non mutable variables are just guarantied to not be modified after initialization, but they can be initialized with any expression.