Skip to content

Tracking issue for exclusive range patterns #37854

Closed

Description

@oli-obk

Summary

This feature allows exclusive integer ranges (such as 0..100) in patterns.

fn is_digit(x: u32) -> bool {
    match x {
        0..10 => true,
        10.. => false,
    }
}

Concerns

History and status

Activity

  1. added
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    on Dec 28, 2016
  2. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Jul 22, 2017
  3. richard-uk1 commented on Jun 5, 2018

    @richard-uk1
    Contributor

    I've just been saved for the second time by the compiler when I did 1 .. 10, but meant 1 ..= 10. It might be worth considering that the compiler won't catch this error if exclusive ranges are allowed.

  4. added a commit that references this issue on Dec 27, 2018
  5. donbright commented on Jan 12, 2019

    @donbright

    seems like exclusive range match would help to improve consistency ... why can i do range in a loop but not in a pattern?

     const MAXN:usize = 14;
     ... 
       for x in 0..MAXN  { blahblahblah(); }
     ...
       let s = match n {
          0..MAXN=>borkbork(),
          MAXN=>lastone(),
          _=>urgh(),
       };
    

    also note that the following is not possible currently because the -1 will break the compile with "expected one of ::, =>, if, or | here"

       let s = match n {
          0..=MAXN-1=>borkbork(),
          MAXN=>lastone(),
          _=>urgh(),
       };
    

    thank you

  6. kentfredric commented on Jun 26, 2019

    @kentfredric

    @donbright I know its a poor consolation, but a good workaround which ensures backwards compatibility might be:

            match i {
                0..=MAXN if i < MAXN => println!("Exclusive: {}", i),
                MAXN => println!("Limit: {}", i),
                _ => println!("Out of range: {}", i)
            }

    playground

    Figured this suggestion might help some poor soul who came here and also battled with not being allowed to use 0..=MAXN-1

  7. bcantrill commented on Jan 16, 2020

    @bcantrill
    Contributor

    It doesn't sound like much is going to happen on this issue, but (FWIW) I would argue against its inclusion in stable Rust: exclusive ranges on pattern matching makes it very easy to introduce bugs, as exclusive ranges are very rarely what the programmer wants in a pattern match (emphasis on match!). In these cases, I would argue that having the programmer be explicit is helpful; there is no reason to offer syntactic sugar in this case. (I landed here because of finding several bugs in a project that had turned this feature on; see tock/tock#1544 for details.)

  8. added a commit that references this issue on Jan 18, 2020
  9. added a commit that references this issue on Jan 18, 2020
  10. 131 remaining items

  11. MultisampledNight commented on Apr 15, 2024

    @MultisampledNight
    Contributor

    I most professionally forgot about this xD. A bit busy right now, I'll try to come back to this in a few weeks.

  12. added a commit that references this issue on May 5, 2024
  13. added a commit that references this issue on May 5, 2024
  14. removed
    S-tracking-needs-summaryStatus: It's hard to tell what's been done and what hasn't! Someone should do some investigation.
    on May 5, 2024
  15. Nadrieril commented on May 5, 2024

    @Nadrieril
    Member

    #124459 is merged, the feature is now stabilized! 🎉 Rust 1.80.0 will support exclusive integer ranges in patterns.

    Thank you to everyone who participated in any way over the 8 years since this feature started.

    @rustbot label +relnotes

  16. added
    relnotesMarks issues that should be documented in the release notes of the next release.
    on May 5, 2024
  17. added a commit that references this issue on May 5, 2024
  18. added a commit that references this issue on May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCF-exclusive_range_pattern`#![feature(exclusive_range_pattern)]`T-langRelevant to the language teamdisposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.relnotesMarks issues that should be documented in the release notes of the next release.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions