Hacker News new | ask | show | jobs
by baseballdork 2171 days ago
> array[i] will check to make sure that array has at least i elements.

At least i+1 elements, right? Or am I getting caught up by one of the three hardest problems again?

3 comments

Nope, you're right, it was me that got caught up by it. :) Thanks, I'll fix that now.

https://github.com/rust-lang/blog.rust-lang.org/commit/fe241... (should roll out in a few minutes)

And this is why we need bounds checking.
The thing that keeps the "two hardest things in CS" joke alive and still funny is that it's so, so true.
IIRC there were two problems that were the hardest:

1. Naming things.

2. Cache invalidation.

3. Off by one errors.

Number 3 apparently the hardest one.

Edit: brain not woken up yet. It’s because of zero indexing.

β€”β€”β€”-

Original question: Out of curiosity, why the +1?

Arrays start at 0. example_array[5] is actually the 6th number in the array.
Ah yeah that makes sense. I was thinking the `i` check already accounted for the zero indexing.
Zero indexing probably
Makes sense. My brains clearly not woken up yet because I thought the `i` check accounted for the zero indexing.