why is safe version of function faster than unsafe?
safe version:
fn from_iterator_safe<T, const N: usize>(mut slice: impl ExactSizeIterator<Item = T>) -> Option<[T; N]> {
\tif N > slice.len() {return None}
\tSome(std::array::from_fn(|_| slice.next().unwrap()))
}
unsafe version:
fn from_iterator<T, const N: usize>(mut slice: impl ExactSizeIterator<Item = T>) -> Option<[T; N]> {
\tif N > slice.len() {return None}
\tSome(std::array::from_fn(|_| unsafe{slice.next().unwrap_unchecked()}))
}
compiler: rustc 1.67.0
compiler flags: -C relocation-model=dynamic-no-pic -C lto=yes -C opt-level="3" -C codegen-units=1 --edition 2021 -C strip="symbols" -C panic=abort