I am writing code that interacts with the NetworkManager over D-Bus and I want to use an enum
using as key-value like scheme.
For example, I want once to use the value 0
as the digit zero and in different context to use the string Unknown
of this enum
:
pub enum NetworkManagerState {
Unknown = 0,
Asleep = 10,
Disconnected = 20,
Disconnecting = 30,
Connecting = 40,
ConnectedLocal = 50,
ConnectedSite = 60,
ConnectedGlobal = 70,
}
What is the cleanest, most idiomatic way of doing so? Is there a way to define a tuple based enum that each entry looks like that (ConnectedSite, 60u32, "Connected")
and use each tuple attribute upon context.
as i32
.cargo.toml:
main.rs:
using the from trait you could make your functions accepting everything that can be converted to your enum: