I just want to start off by saying I'm very new to Rust and I'm currently in the early stages of learning and couldn't find a clear explanation on google.
I have an simple enum mapped to integers (not sure how enum with values are called). I'm trying to add a bunch of them together to figure out the count. I'm currently doing this by defining a value function, but this seems error prone to me since I need to repeat the mapping to values. Is there a better way of doing this ? I understand that I would need a function to convert from i32 to Result to account for numbers outside of my enum range, but I feel there must be something to do computation (similar to how I print the value using as i32. (sorry English is not my primary language)
example:
enum Result {
Lose = -1,
Draw = 0,
Win = 1,
}
impl Result {
fn value(&self) -> i32 {
match &self {
Result::Win => 1,
Result::Draw => 0,
Result::Lose => -1,
}
}
}
fn main() {
let a = Result::Win;
let b = Result::Lose;
let c = a.value() + b.value();
println!("results are a={} and b={} and c={}", a as i32, b as i32, c);
}
}