rust-lang / rust-lang/rust

core lib request: Add a count/length method to `core::range::Range`

Open Beginner friendly
#161,109 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

F-new_range needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The old core::ops::Range was an iterator directly, so you could check the count with r.count(). On the new range type you can't easily check the count / length of the range (eg: to compare it to the length of a string when you're unit testing your parser). writing r.into_iter().count() seems a little overkill to me.

I do understand that the point of the new Range is to not natively be an iterator so that it can be Copy, but it still logically has a length to it, and checking that length is a non-destructive step, so having this method built directly onto the type shouldn't break anyone's assumptions on accident, and it seems completely in the spirit of the existing is_empty method.

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 core::range::Range entry point and compare the requested query with the existing is_empty method. Resolve whether the API should be named count or length and define its behavior for representative ranges; done means the non-destructive query is available with coverage for those cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.