DEV Community

loading...

Discussion on: Rust and Go department directories

Collapse
harmic profile image
Mike Harris

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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.