How do I convert an enum reference to a number?

*foo as u8 is correct, but you have to implement Copy because otherwise you would leave behind an invalid reference.

#[derive(Copy, Clone)]
enum Foo {
    Bar = 1,
}

fn f(foo: &Foo) -> u8 {
    *foo as u8
}

Since your enum will be a very lightweight object you should pass it around by value anyway, for which you would need Copy as well.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)