Exhaustive integer matching

main--

I try to exhaustively match integers like this:

fn main() {
    for test in range(std::u8::MIN, std::u8::MAX) {
        match test {
            0x00..0xff => {},
        }
    }
}

But the compiler complains:

all.rs:3:9: 6:10 error: non-exhaustive patterns: `_` not covered [E0004]
all.rs:3         match test {
all.rs:4             0x00..0xff => {},
all.rs:5         }
error: aborting due to previous error

However, all possible values are covered. A quick check confirms this:

fn main() {
    for test in range(std::u8::MIN, std::u8::MAX) {
        match test {
            0x00..0xff => {},
            _ => fail!("impossible"),
        }
    }
}

And now compile and run it:

$ rustc all.rs
$ ./all
$

All possible values are covered, so why does rustc still want the obviously unreachable _ arm?

varkor

Exhaustive integer matching was stabilised in Rust 1.33.0. The original example in the question now works (updated to use the modern syntax for ranges).

fn main() {
    for i in std::u8::MIN..=std::u8::MAX {
        match i {
            0x00..=0xff => {}
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

this pattern-matching is not exhaustive in OCaml

From Dev

How to fix the pattern-matching exhaustive warning?

From Dev

Non-exhaustive pattern matching in Haskell

From Dev

Haskell pattern matching - Non-exhaustive pattern - but why?

From Dev

Matching sets of integer literals

From Dev

Matching positive integer with haskell

From Dev

Large Integer ID matching

From Dev

SQLite matching on partial integer

From Dev

integer-matching regex pattern not working

From Dev

Regex for Matching a Floating Point Number which is not an Integer

From Dev

FHIR: Treating identifier.value as an integer for matching

From Dev

Spring JUnit Test - JSONPath for Integer not Matching

From Dev

Total all matching integer elements in an array

From Dev

Compare two integer and count matching numbers

From Dev

Is There An Exhaustive Profiler?

From Dev

Match non exhaustive in sml

From Dev

Compiler error for exhaustive switch

From Dev

Force exhaustive switch

From Dev

Exhaustive combinations of lists in python

From Dev

Exhaustive properties in Haskell QuickCheck?

From Dev

Fetch matching results from the integer array satisfying the condition which is given as text

From Dev

Type to capture either integer, float or a string value and then do pattern matching in Scala

From Dev

Non exhaustive pattern in function in GHCi

From Dev

Why is this a non exhaustive search pattern?

From Dev

Non exhaustive pattern in function noThirds

From Dev

Scala warning match may not be exhaustive

From Dev

Haskell: non-exhaustive-patterns

From Dev

Exhaustive condition of switch case in Swift

From Dev

Creating an exhaustive list of `n` substrings

Related Related

HotTag

Archive