Programmatically convert enum with associated data to repr value
⚓ Rust 📅 2025-07-16 👤 surdeus 👁️ 20If I have
#[repr(u8)]
enum Foo {
Bar = 0x01,
Spamm = 0x02,
}
Then I can get the variant's associated value via
let e = Foo:: Bar;
let val = e as u8;
However, this naive approach does not work when the enum has associated data:
#[repr(u8)]
enum Foo {
Bar(u16) = 0x01,
Spamm(String) = 0x02,
}
Is there a native way to get the associated tag value of the enum, short of using a macro or third-party library?
4 posts - 2 participants
🏷️ rust_feed