As of 0.1.38, num_rational::Ratio implements PartialEq and Eq by the numeric value, while Hash is derived. This violates the rule of Eq + Hash which is a == b ⇒ hash(a) == hash(b) when non-reduced ratio is involved:
let a = Rational::new_raw(4, 2);
let b = Rational::new_raw(6, 3);
assert_eq!(a, b); // ok.
let mut h = HashSet::new();
h.insert(a);
h.insert(b);
assert_eq!(h.len(), 1); // likely panic, 2 ≠ 1
The hash should be computed using the reduced value. This unfortunately means the GCD will need to be calculated every time we need the hash.
As of 0.1.38,
num_rational::RatioimplementsPartialEqandEqby the numeric value, whileHashis derived. This violates the rule of Eq + Hash which isa == b ⇒ hash(a) == hash(b)when non-reduced ratio is involved:The hash should be computed using the reduced value. This unfortunately means the GCD will need to be calculated every time we need the hash.