#values #enumerate #information #helping

可枚举

一个帮助您枚举类型所有可能值的库

5 个版本 (1 个稳定版本)

1.0.0 2024年6月26日
0.3.0 2024年6月5日
0.2.0 2024年4月27日
0.1.0 2024年4月8日

#463Rust 模式

MIT 许可证

36KB
602

可枚举

枚举类型的所有可能值。

请参阅 示例 获取更多示例和如何使用此crate的指南。

请参阅 文档 获取有关此crate的详细信息。

TL;DR

use enumerable::Enumerable;

#[derive(Debug, Copy, Clone, Enumerable)]
#[allow(dead_code)]
enum Food {
    Apple,
    Banana,
    Coffee { with_milk: bool },
}

#[derive(Debug, Copy, Clone, Enumerable)]
#[allow(dead_code)]
struct Meal {
    alice_eats: Food,
    bob_eats: Option<Food>,
    at_home: bool,
}

fn main() {
    println!("There are {} different meals, enumerated as follows:", Meal::ENUMERABLE_SIZE);
    for meal in Meal::enumerator() {
        println!("{:?}", meal);
    }
}

上面的代码将输出

There are 40 different meals, enumerated as follows:
Meal { alice_eats: Apple, bob_eats: None, at_home: false }
Meal { alice_eats: Apple, bob_eats: None, at_home: true }
Meal { alice_eats: Apple, bob_eats: Some(Apple), at_home: false }
Meal { alice_eats: Apple, bob_eats: Some(Apple), at_home: true }
Meal { alice_eats: Apple, bob_eats: Some(Banana), at_home: false }
Meal { alice_eats: Apple, bob_eats: Some(Banana), at_home: true }
Meal { alice_eats: Apple, bob_eats: Some(Coffee { with_milk: false }), at_home: false }
Meal { alice_eats: Apple, bob_eats: Some(Coffee { with_milk: false }), at_home: true }
Meal { alice_eats: Apple, bob_eats: Some(Coffee { with_milk: true }), at_home: false }
Meal { alice_eats: Apple, bob_eats: Some(Coffee { with_milk: true }), at_home: true }
Meal { alice_eats: Banana, bob_eats: None, at_home: false }
...

依赖关系

~1.3–2MB
~41K SLoC