With regard to your comments about indexing operations panicking when out of bounds, this makes an interesting read. In that context indexing out of bounds would be considered a programmer error, so panicking is the appropriate action.
Also consider how painful it would be to use if it did return an Option - in most cases you would end up liberally sprinkling unwrap or expect all over the place. It would be especially weird where an indexed array is on the LHS of an assignment:
for x = 0 .. somevalue {
for y = 0 .. someothervalue {
sum_elements[x].unwrap() = rows[x].unwrap()+cols[y].unwrap();
}
}
That is not valid syntax, of course. container[index] is actually syntactic sugar for *container.index_mut(index) which could not work if index_mut returned an option.
For Slices there is also get_unchecked, an unsafe method which does no bounds checking.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
With regard to your comments about indexing operations panicking when out of bounds, this makes an interesting read. In that context indexing out of bounds would be considered a programmer error, so panicking is the appropriate action.
Also consider how painful it would be to use if it did return an Option - in most cases you would end up liberally sprinkling
unwrap
orexpect
all over the place. It would be especially weird where an indexed array is on the LHS of an assignment:That is not valid syntax, of course.
container[index]
is actually syntactic sugar for*container.index_mut(index)
which could not work ifindex_mut
returned an option.For Slices there is also
get_unchecked
, an unsafe method which does no bounds checking.