2 ms·
The implementation of bounds checking couldn't be written any better: https://doc.rust-lang.org/1.37.0/src/core/slice/mod.rs.html#2643-2689 https://doc.rust-lan
by steveklabnik 7y ago
The implementation of bounds checking couldn't be written any better: https://doc.rust-lang.org/1.37.0/src/core/slice/mod.rs.html#2643-2689 https://doc.rust-lang.org/1.37.0/src/core/slice/mod.rs.html#...
#[inline]
fn get(self, slice: &[T]) -> Option<&T> {
if self < slice.len() {
unsafe {
Some(self.get_unchecked(slice))
}
} else {
None
}
}
This is exactly how you'd write the feature, by hand, if you were implementing the language.
That the optimizer could, but does not, do as much optimization as it theoretically can, means that it has more work to do. But that's different than the feature being written in a sub-optimal way.
- michael_j_ward 7y agoI don't know if you edited your comment, or if it was just my pre-coffee reading comprehension missed the part "because the compiler isn’t always removing bounds checks that are provably true." I understand now, gracias.
- steveklabnik 7y agoI don't think I did, but I am very empathetic to pre-coffee reading comprehension woes :) Glad we got it sorted.