2 ms·
> But (if I understand correctly) these are checks implicitly added by the compiler. This is a dubious statement. In Rust, the array indexing operator arr[i] i
by NobodyNada 1y ago
> But (if I understand correctly) these are checks implicitly added by the compiler.
This is a dubious statement. In Rust, the array indexing operator arr[i] is syntactic sugar for calling the function arr.index(i), and the implementation of this function on the standard library's array types is documented to perform a bounds-check assertion and access the element.
So the checks aren't really implicitly added -- you explicitly called a function that performs a bounds check. If you want different behavior, you can call a different, slightly-less-ergonomic indexing function, such as `get` (which returns an Option, making your code responsible for handling the failure case) or `get_unchecked` (which requires an unsafe block and exhibits UB if the index is out of bounds, like C).
- nubbler 1y agoAnother commenter in this thread used the phrase "complex abomination" which seems more and more apt the more I learn about Rust.
- J_Shelby_J 1y agoNothing in this world is perfect, but this behavior is less of an abomination than whatever a junior dev on a timeline might write to handle this condition.