0

Is it possible to cast an int to a C-like enum in rust? It's possible to cast from the enum to the int but I'd like to do it the other way too.

enum Type {
    FIRST = 1,
    SECOND = 2,
    THIRD = 3,
}

fn main() {
    let x = 3u8 as Type;
    println!("{}", x as u8);
}

Results in an error trying to cast to Type:

main.rs:8:13: 8:24 error: non-scalar cast: `u8` as `Type`
main.rs:8     let x = 3u8 as Type;
                      ^~~~~~~~~~~
error: aborting due to previous error
J V
  • 11,402
  • 10
  • 52
  • 72

0 Answers0