

Rust does not check arrays at compile time if it cannot know the index at compile time, for example in this code:
fn get_item(arr: [i32; 10]) -> i32 {
let idx = get_from_user();
arr[idx] // runtime bounds check
}
When it can know the index at compile time, it omits the bounds check, and iterators are an example of that. But Rust cannot always omit a bounds check. Doing so could lead to a buffer overflow/underflow, which violates Rust’s rules for safe code.
Edit: I should also add, but the compiler also makes optimizations around slices and vectors at compile time if it statically knows their sizes. Blanket statements here around how it optimizes will almost always be incorrect - it’s smarter than you think, but not as smart as you think at the same time.
(Yes)