Closed
Description
slice::binary_search
(and perhaps its cousins?) behave in a fairly unexpected (and undocumented) way in the presence of duplicates:
let s = &[0, 1, 1, 1, 1, 2];
// What I would expect:
assert_eq!(s.binary_search(&1), Ok(1));
// Actual result:
assert_eq!(s.binary_search(&1), Ok(4));
While not strictly a bug (since the documentation does not state a particular behavior in this case) it would be fairly simple to tweak the implementation to provide the more expected behavior of returning the first match. Either way, it would be preferable if the behavior were documented.
Making the change at this point might result in some unexpected code breakage, even though the behavior is not documented. In order to ease the transition, it may be best to rename the current implementation to reverse_binary_search
or similar, and provide the expected implementation for binary_search
.