> OutOfBounds errors aren't checked at compile time unless it's a `const` type IIRC.
There are circumstances where Rust can calculate the array index at compile time and, if it can, it will result in a compilation error if the index is out of bounds. Of course, this is a best-effort analysis and doesn't work for all possible cases. Here's some Rust code, the first three indexing operations fail at compile-time, but the last doesn't:
let x = [1,2,3];
x[4]; // compile error
const FOUR: usize = 4;
x[FOUR]; // compile error
let y = 4;
x[y]; // compile error
fn foo() -> usize { 4 }
x[foo()]; // runtime error
This analysis works on fixed-size arrays, but not on Vec.
kibwen|4 years ago
There are circumstances where Rust can calculate the array index at compile time and, if it can, it will result in a compilation error if the index is out of bounds. Of course, this is a best-effort analysis and doesn't work for all possible cases. Here's some Rust code, the first three indexing operations fail at compile-time, but the last doesn't:
This analysis works on fixed-size arrays, but not on Vec.