Skip to content

Pattern mathing and fixed size decimals #29424

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

Closed
quasilyte opened this issue Oct 28, 2015 · 1 comment
Closed

Pattern mathing and fixed size decimals #29424

quasilyte opened this issue Oct 28, 2015 · 1 comment

Comments

@quasilyte
Copy link

The following code is not compiling, yet looking correct from my point of view:

println!("{}", match 777_i64 /* any i64 value will do */ {
        i64::MIN...i64::MAX => "valid i64 value"
});

This does, but `_' here is kinda redundant:

println!("{}", match 777_i64 /* any i64 value will do */ {
        i64::MIN...i64::MAX => "just as expected",
        _ => "how this can possibly happen?"
});

Expected result: better match exhaustiveness checking or explicit reference to this kind of problems in E0004 (at least until this gets fixed, or forever, if this is not going to be fixed).

Details

To be more precise, by fixed size decimals I mean i/ui{8, 16, 32, 64}.
Currently in Rustc 1.3.0 (stable channel) user can not satisfy the compiler
even when whole range of possible [literal] values are covered in match pattern.

Lets imagine some of the if/else chain:

trait SubjectiveMeasurement {
    fn how_big(self) -> &'static str;
}

impl SubjectiveMeasurement for i64 {
    fn how_big(self) -> &'static str {
        if self < -10000 {
            "number is really tiny"
        } else if self < -1000 {
            "number is tiny"
        } else if self < 0 {
            "number is small"
        } else if self == 0 {
            "number is nil"
        } else if self > 10000 {
            "number is really huge"
        } else if self > 1000 {
            "number is huge"
        } else {
            "number is big"
        }
    }
}

fn main() {
    println!("{}", (-1000_i64).how_big());
    println!("{}", (1000_i64).how_big());
}

We can use pattern matching by ranges here to get better readability:


impl SubjectiveMeasurement for i64 {
    fn how_big(self) -> &'static str {        
        match self {
            i64::MIN...-10000 => "number is really tiny",
            -9999...-1001 => "number is tiny",
            -1000...-1 => "number is small",
            0 => "number is nil",
            1...1000 => "number is big",
            1001...9999 => "number is huge",
            10000...i64::MAX => "number is really huge",
            _ => "why the hell I need this arm?!"
        }
    }
}

But again, we need an extra `_' arm.
From a position of compiler user, not a compiler developer, I want it to do a better job at
checking are all cases guarded or not.

Meta

quasilyte@lb:~$ rustc --version --verbose
rustc 1.3.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.3.0
@jonas-schievink
Copy link
Contributor

Dupe of #12483

@arielb1 arielb1 closed this as completed Oct 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants