enums - 按范围匹配枚举

标签 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

那么,这样的事情有可能实现吗?

最佳答案

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

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

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

关于enums - 按范围匹配枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26894244/

相关文章:

c - Clang 编译器的 C 枚举的数据类型是什么?

c++ - C++ 中的枚举 - 函数如何赋值

grails - 在Grails g:select中,使用枚举显示枚举键,但将这些值用作选项的值

collections - 如何在迭代集合的同时修改集合?

C# - 将一个枚举转换为另一个

python - 枚举中有大量常量

rust - 在 Rust 中查找字素簇的大小

rust - 如何处理或测试某个类型是否是 Rust 宏中的选项?

rust - "borrowed value does not live long enough"好像怪错了

rust - 将 `Vec<u16>` 内容写入文件的正确方法是什么?