Taking a generic enum or any type that can do "as usize" in a function

Viewed 607

I don't know what the answer to this would look like and that's why the title is so ambiguous.

It is possible in rust to cast enum types to numbers using "as" like so

enum ExampleEnum { A, B, C }

fn main() {
    println!("{}", ExampleEnum::B as usize);
}

Is there a way to make a generic function that can take any data type that can do that? Or some Trait that encapsulates that functionality? Maybe in an "unsafe" way (transmute?) ?

enum ExampleEnum { A, B, C }

fn what_n(x: ???) -> usize {
    x as usize
}

fn main() {
    println!("{}", what_n(ExampleEnum::B));
}
0 Answers
Related