3 ms·
I get your point, but you have cardinality(usize) elements in your array. So you need to use an indexing type and operations that let you exploit that full car
by rstuart4133 5y ago
I get your point, but you have cardinality(usize) elements in your array. So you need to use an indexing type and operations that let you exploit that full cardinality.
usize does have the required range, but then you combine it with std::ops::Add(). In this case std::ops::Add()'s overflow check just gets in the way, preventing you from fearlessly exploiting all 64 bits in the usize. Had you used usize.wrapping_add(), it would not only have not hit the overflow panic, it would have been faster.
Maybe that's your point? The final version of his code is indeed buggy, because middle should have been calculated like this:
let middle = high.wrapping_add(low) / 2;