Ask coding questions

← Back to all posts
Rust How to convert enum to underlying type
xxpertHacker (861)

How could I losslessly convert an enum to it's underlying repr type?

as casting is usually used when someone doesn't care about info loss, wrapping, etc.

I would have hoped that the trait From/Into would be implemented for converting the enum to the underlying type, but that wouldn't make sense, as the type is not always convertible to the enum, even if the enum is convertible to the repr type.

Does anyone have any idea as to how this should be done?