Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Rust check array bounds at runtime, when (most) other checks occur at compile time?

Tags:

People also ask

Does Rust Check array bounds?

Rust doesn't always perform a bounds check. I think iterating over an array doesn't result in bounds check for every item. And if you need to access individual items by index, there are unsafe functions for unchecked access.

Does C++ do bounds checking?

This is due to the fact that C++ does not do bounds checking. Languages like Java and python have bounds checking so if you try to access an out of bounds element, they throw an error. C++ design principle was that it shouldn't be slower than the equivalent C code, and C doesn't do array bounds checking.


Reading the basic introduction:

If you try to use a subscript that is not in the array, you will get an error: array access is bounds-checked at run-time.

Why does Rust check array bounds at runtime, when it seems most other checks occur at compile time?