Note that this verbosity issue can be resolved by building an unsafe_indexing!
macro that 1/wraps a code block in unsafe and 2/replaces every occurrence of something[i]
with something.get_unchecked(i)
. For all I know, there might even already be a crate for that.
On the other hand, Rust makes unchecked indexing unsafe and noisy for a reason. Every call to get_unchecked()
introduces a possible memory safety bug, which Rust is supposed to prevent.
Which is why the safe solution proposed by @kornel is usually favored, unless one is really operating inside of a hot loop where even the "outer" bound check already has unacceptable overhead.
Let's cross fingers that someday, rustc and LLVM will learn to do this bound check optimization automatically, instead of having us programmers write weird slicing incantations in order to benefit from it.