I am using the slice binary search in a small project, but I require that it returns the last index of the matching elements. Checking the stdlib src I found that this is actually the implemented behaviour. But the docs state that it could return any of the matching positions.
I also browsed trough this PR https://github.com/rust-lang/rust/pull/45333 and noticed there's even this test that checks this specific functionality:
https://github.com/rust-lang/rust/blob/master/src/libcore/tests/slice.rs#L72
#[test]
// Test implementation specific behavior when finding equivalent elements.
// It is ok to break this test but when you do a crater run is highly advisable.
fn test_binary_search_implementation_details() {
let b = [1, 1, 2, 2, 3, 3, 3];
assert_eq!(b.binary_search(&1), Ok(1));
assert_eq!(b.binary_search(&2), Ok(3));
assert_eq!(b.binary_search(&3), Ok(6));
let b = [1, 1, 1, 1, 1, 3, 3, 3, 3];
assert_eq!(b.binary_search(&1), Ok(4));
assert_eq!(b.binary_search(&3), Ok(8));
let b = [1, 1, 1, 1, 3, 3, 3, 3, 3];
assert_eq!(b.binary_search(&1), Ok(3));
assert_eq!(b.binary_search(&3), Ok(8));
}
The comment basically indicates that if someone relies on this functionality (assuming it's tests would catch it) it would be considered required and not change it?
Why not change the documentation instead and commit to it?
Knowing this, should I just add a test that checks this behaviour and perhaps fail on later rust versions, copy the current implementation into my src (unsafe calls), or open an issue and try to force the stdlib to pick one of the behaviours and adjust docs accordingly.