1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use enumerable::Enumerable;
pub fn main() {
// The `Enumerable` trait allows you to enumerate all possible values of a type. It is
// implemented for most primitive types, and you can derive it for your own types.
//
// To enumerate all possible values of a type implementing the `Enumerable` trait, call
// `Enumerator::enumerator()`, which will give you an iterator.
//
// The following primitive types implement `Enumerable`:
// - i8, i16, i32, i64, i128, isize
// - u8, u16, u32, u64, u128, usize
// - char
// - bool
// - ()
//
// For example, to list all possible values of `bool`, you can do:
println!(
"all possible values of bool : {}",
bool::enumerator()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(", ")
);
// You can also use `<T as Enumerable>::enumerator()` explicitly:
println!(
"first 10 possible values of u8 : {}",
<u8 as Enumerable>::enumerator()
.take(10)
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(", ")
);
// `Enumerable` also provides an associated constant named `ENUMERABLE_SIZE` which gives the
// number of possible values of the type.
println!(
"count of the possible values of u8 : {}",
u8::ENUMERABLE_SIZE
);
// which is equivalent but more efficient than counting the iterator:
println!(
"counting it in a inefficient way : {}",
u8::enumerator().count()
);
// However, if the number of possible values exceeds `usize::MAX`, `ENUMERABLE_SIZE` will panic
// at compile time. If that's not the desired behavior, use `ENUMERABLE_SIZE_OPTION` instead,
// which will give you a `None` in that case.
println!(
"count of the possible values of i32 : {:?}",
i32::ENUMERABLE_SIZE_OPTION
);
println!(
"count of the possible values of i64 : {:?}",
i64::ENUMERABLE_SIZE_OPTION
);
// `()` is also implemented, and it has only one possible value, `()`.
println!(
"all possible values of () : {}",
<() as Enumerable>::enumerator()
.map(|u| format!("{:?}", u))
.collect::<Vec<_>>()
.join(", ")
);
// `Enumerable::enumerator()` returns a iterator, so you can use all iterator methods on it.
println!(
"i32 from smallest, step 16, skip 10 : {}, ...",
i32::enumerator()
.step_by(16)
.skip(10)
.take(5)
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(", ")
);
}