按范围匹配枚举

时间:2014-11-12 18:45:13

标签: enums rust

我想知道是否可以使用带枚举的范围匹配器。玩具示例:

enum Things {
    One,
    Two,
    Three
}

pub fn main() {
    match One {
        One...Two => println!("one to two"),
        Three => println!("three")
    }
}

错误:

<anon>:9:9: 9:12 error: only char and numeric types are allowed in range [E0029]
<anon>:9         One...Two => println!("one to two"),
                 ^~~
error: aborting due to previous error
playpen: application terminated with error code 101

http://is.gd/rxKMfk

那么,是否有可能做出像这样的工作?

1 个答案:

答案 0 :(得分:5)

不是真的。枚举没有订购。但是,您可以这样做:

enum Things {
    One = 1,
    Two = 2,
    Three = 3
}

pub fn main() {
    match One as uint {
        1..2 => println!("one to two"),
        3 => println!("three")
    }
}