Suppose you want to define a lookup table. Naively it could be achieved this way:
#[allow(dead_code)]
enum E{A,B,C,D}
static LOOKUP: [i32;4] = [1,2,3,4];
fn main() {
println!("{}",LOOKUP[E::A as usize]);
}
But actually we want to express a strict relationship between the enumeration and the lookup table:
enum E{A,B,C,D}
static LOOKUP: [i32;4] = [
E::A as usize: 1,
E::B as usize: 2,
E::C as usize: 3,
E::D as usize: 4
];
fn main() {
println!("{}",LOOKUP[E::A as usize]);
}
Additionally, a purified variant could be provided:
enum E{A,B,C,D}
static LOOKUP: [i32;E] = [A: 1, B: 2, C: 3, D: 4];
fn main() {
println!("{}",LOOKUP[E::A]);
}