← Back to context

Comment by ViewTrick1002

12 hours ago

The difference is the checking, and actual enforcement of it.

Go and use get_unchecked if you want to and get C like behavior. But the safety note tells you the potential issues:

Safety

Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.

You can think of this like .get(index).unwrap_unchecked(). It’s UB to call .get_unchecked(len), even if you immediately convert to a pointer. And it’s UB to call .get_unchecked(..len + 1), .get_unchecked(..=len), or similar.

https://doc.rust-lang.org/std/vec/struct.Vec.html