News for hackers. Stuff that matters.
Re:Grids in Rust, Part 2: const generics by kibwen on Saturday April 03, 2021 @07:50PM (#26685782)
> 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.

There are no comments on this comment yet.