Why does rust have [ ] indexing?

While it's important to offer a way to do things that doesn't panic, it's also important to think about what people will realistically do on errors. If something like 99% of the time people will just say "shut up compiler; I checked that already", then it can actually be better for the short-named one to be the panicking one, with a separate try_blah-style method available as an option.

Note that this is generally only true when it's easy to http://wiki.c2.com/?LookBeforeYouLeap -- like it is with array indexing, where algorithms using indexes generally already ensure they're in-range. For things where checking the precondition is difficult or impossible, it's better to http://wiki.c2.com/?CoupleLeapingWithLooking and not have the panic-by-default design.

(Obligatory link: Not Explicit)

8 Likes