【问题标题】:match enum by range按范围匹配枚举
【发布时间】:2014-11-12 18:45:13
【问题描述】:

我想知道是否可以将范围匹配器与枚举一起使用。玩具示例:

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

那么,有没有可能做这样的事情?

【问题讨论】:

  • 不是你要的,但你可以在同一个分支中匹配两个枚举:One | Two =&gt; ...

标签: enums rust


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 2018-08-06
    相关资源
    最近更新 更多