diff --git a/library/alloc/src/vec.rs b/library/alloc/src/vec.rs index c54b3aef95ed4..e973c3287ede0 100644 --- a/library/alloc/src/vec.rs +++ b/library/alloc/src/vec.rs @@ -2985,8 +2985,14 @@ impl Iterator for IntoIter { where Self: TrustedRandomAccess, { - // SAFETY: the caller must uphold the contract for - // `Iterator::get_unchecked`. + // SAFETY: the caller must guarantee that `i` is in bounds of the + // `Vec`, so `i` cannot overflow an `isize`, and the `self.ptr.add(i)` + // is guaranteed to pointer to an element of the `Vec` and + // thus guaranteed to be valid to dereference. + // + // Also note the implementation of `Self: TrustedRandomAccess` requires + // that `T: Copy` so reading elements from the buffer doesn't invalidate + // them for `Drop`. unsafe { if mem::size_of::() == 0 { mem::zeroed() } else { ptr::read(self.ptr.add(i)) } }