rust-lang / rust-lang/rust

Make clarifications for `binary_search` API

Open
#124,882 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-docs C-discussion T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Location

https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search

Summary

Documentation states, that

If the slice is not sorted, the returned result is unspecified and meaningless.

but if we try to use binary search on something like [5,4,3,2,1].binary_search(&5) it will end up with error, though the slice is sorted in descending order.
This either requires to fix docs to something like

If the slice is not sorted in ascending order, the returned result is unspecified and meaningless.

or fix actual binary search algorithm by flipping direction of search in case left and right bounds are flipped.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the linked std::slice::binary_search documentation and review the wording about sorted slices against the reported descending-order example. Clarify the documented ordering requirement if that is the intended scope; the issue also raises an alternative algorithm change, so confirm the expected resolution before editing.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.