Skip to content

Tracking issue for exclusive range patterns #37854

Closed
@oli-obk

Description

@oli-obk
Contributor

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

added
B-unstableBlocker: Implemented in the nightly compiler and unstable.
on Dec 28, 2016
added
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
on Jul 22, 2017
ghost

ghost commented on Jul 30, 2017

@ghost

EDIT: the following is no longer true 12 jan 2019 nightly debug 2018:

Missing warning: unreachable pattern in the case when 1...10 then 1..10 are present in that order. That warning is present in case of 1..12 then 1..11 for example.

Example (playground link):

#![feature(exclusive_range_pattern)]
fn main() {
    let i = 9; //this hits whichever branch is first in the match without warning
    match i {
        1...10 => println!("hit inclusive"),
        1..10 => println!("hit exclusive"),//FIXME: there's no warning here
        // ^ can shuffle the above two, still no warnings.
        1...10 => println!("hit inclusive2"),//this does get warning: unreachable pattern
        1..10 => println!("hit exclusive2"),//this also gets warning: unreachable pattern

        _ => (),
    };

    //expecting a warning like in this:
    match i {
        1..12 => println!("hit 12"),
        1..11 => println!("hit 11"),//warning: unreachable pattern
        _ => (),
    }
}
richard-uk1

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.

added a commit that references this issue on Dec 27, 2018
donbright

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

added a commit that references this issue on May 27, 2019
kentfredric

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

183 remaining items

Loading
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

        Participants

        @avl@kentfredric@nikomatsakis@joshtriplett@pnkfelix

        Issue actions

          Tracking issue for exclusive range patterns · Issue #37854 · rust-lang/rust