In Java, I've this:
enum TradingDays {
Monday(2), Tuesday(3), Wednesday(4), Thursday(5), Friday(6);
private int value;
private TradingDays(int value) {
this.value = value;
}
};
That is called as:
TradingDays.Tuesday.value
Can I make same in rust?
kindof
enum Day {
Monday, Tuesday, Wednesday, Thursday, Friday
}
impl Day {
fn value(&self) -> u32 {
use self::Day::*;
match self {
Monday => 2,
Tuesday => 3,
Wednesday => 4,
Thursday => 5,
Friday => 6
}
}
}
and you can use it like
Day::Tuesday.value()
2 Likes
In this specific case, you can also assign the integers to the variants:
enum Day {
Monday = 2,
...
}
...
let value = Day::Monday as u32;
3 Likes