Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> A practical problem is writing correct bounds checks. For example, consider the get_unchecked function on slice - it says “an out-of-bounds index is undefined behavior” but never defines what out of bounds means. So how does one even call this function safely?

Except that it's well defined: If you start or end after then end of the array/slice/vector. The same way it's done for any other array/slice/vector access.

Could the documentation of this nightly experimental API be better, sure. Is it undefined or unusable? No it's as well defined as "an array index being out of bounds".

Oh and it's only defined for Range<usize> before you wonder there is no confusion with any ranges of custom types, this a method of a trait implemented for only for Range<usize> which generalizes the indexing of slices and of which everything but the name is currently unstable/experimental.



Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: