Skip to content

Improve doc of some methods that take ranges #140983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

tkr-sh
Copy link
Contributor

@tkr-sh tkr-sh commented May 13, 2025

Some methods that were taking some range in parameter were a bit inconsistent / unclear in the panic documentation.

Here is the recap:

  • Replaced "start/end point" by "start/end bound" to be coherent with RangeBounds naming (it's also easier to understand I think)
  • Previously, it was written "[...] or if the end point is greater than the length of [...]", but this is not entirely true! Actually, you can have a start bound that is greater than the length, with an end bound that is unbounded and it will also panic. Therefore I think that "[...] one of the range bound is bounded and greater than the length of [...]" is better!
  • String methods weren't mentionning that the method panics if start_bound > end_bound but it actually does! It uses slice::range which panics when start > end. (https://doc.rust-lang.org/stable/src/alloc/string.rs.html#1932-1934, https://doc.rust-lang.org/stable/src/core/slice/index.rs.html#835-837).
    You can also test it with:
struct MyRange;
impl std::ops::RangeBounds<usize> for MyRange {
    fn start_bound(&self) -> std::ops::Bound<&usize> {
        std::ops::Bound::Included(&3usize)
    }
    fn end_bound(&self) -> std::ops::Bound<&usize> {
        std::ops::Bound::Included(&1usize)
    }
}

fn main() {
    let mut s = String::from("I love Rust!");
    s.drain(MyRange); // panics!
}

@rustbot
Copy link
Collaborator

rustbot commented May 13, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 13, 2025
@tkr-sh
Copy link
Contributor Author

tkr-sh commented May 13, 2025

@rustbot label +A-docs

@rustbot rustbot added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label May 13, 2025
@tkr-sh
Copy link
Contributor Author

tkr-sh commented May 27, 2025

r? @hkBst

@rustbot
Copy link
Collaborator

rustbot commented May 27, 2025

Failed to set assignee to hkBst: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

Copy link
Member

@hkBst hkBst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with this.

Comment on lines +1461 to +1462
/// Panics if the range has `start_bound > end_bound`, or, a range bound is
/// bounded and greater than the length of the deque.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

", or, a range bound is bounded and greater than ..." doesn't read quite right to me. Suggestion: ", or if the range has an end bound that is greater than the length of the dequeu".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tgross35 the problem with that is that it's not as precise as the previous one for 2 reasons:

  • If the end bound is unbounded, do we consider that greater than the length ? Unbounded kind of means "infinite" so you could understand from your suggestion that if it's unbounded it will panic which is false
  • The method can also panic if it's the start bound that is greater than the length of the deque

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • If the end bound is unbounded, do we consider that greater than the length

Users are more likely to think of things with regard to the range types, which may have "no end bound", not an "unbounded end bound" which this current PR's wording matches. I assume this comes from RangeBounds::end_bound always returning a Bound { Included, Excluded, Unbounded }; this type is better thought of as a flattened form of Option<enum { Included, Excluded }>, however, it is possible for an impl RangeBounds type to have "no end bound" (the docs for Unbounded say "Indicates that there is no bound in this direction.", after all).

The method can also panic if it's the start bound that is greater than the length of the deque

", or if the range has an end a start or end bound that is greater than the length of the dequeu" then? I'm open to other suggestions, but the "range bound is bounded" bit should change. (Also s/, or,/, or if)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can kind of agree with the first point, tho I'm not sure how you can put it into words so it's easier to understand.

And for the second point, well then:

, or if the range has a start or end bound that is greater than

v

, or if the range has a bound that is greater than

v

, or if a range bound is greater than

which is almost the same thing as the current documentation without the "is bounded" part (tho I agree on the sed part)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants