Hi all! I had an issue from my starts with Rust, the issue is not about rust, is related to match and biyective relations, by bijective I means, two groups with elements, one element of one group is always a match to only one of the other group, so you can move from the group A to B, and B to A always by the same element.
enum Word {
A,
B,
C,
}
enum Number {
Zero,
One,
Two,
}
fn letter2number(word: Word) -> Number {
match word {
Word::A => Number::Two,
Word::B => Number::One,
Word::C => Number::Zero,
}
}
fn number_to_word(number: Number) -> Word {
match number {
Number::Zero => Word::C,
Number::One => Word::B,
Number::Two => Word::A,
}
}
Bijective relations allow us to go from one to other in a clear way, they also have the full information for match
, this is a simple case, but more complex we need to make a test to always prove the conversion Word -> Number -> Word is always the same to be consistent.
Is easier to make a table of relations for example, a const one, but that implies do not use match, but match is nice because if anything changes, rust will not compile until we handle the missed ones.
But maybe, there already exists a crate or other way to handle bijetive patters, to avoid issues of consistency.
Thx!