diff --git a/rational/src/lib.rs b/rational/src/lib.rs index 938de46..6349266 100644 --- a/rational/src/lib.rs +++ b/rational/src/lib.rs @@ -261,6 +261,13 @@ impl Ratio { } } +// From integer +impl From for Ratio where T: Clone + Integer { + fn from(x: T) -> Ratio { + Ratio::from_integer(x) + } +} + // Comparisons // Mathematically, comparing a/b and c/d is the same as comparing a*d and b*c, but it's very easy @@ -738,6 +745,7 @@ mod test { assert_eq!(_1_2, Ratio::new(1, 2)); assert_eq!(_3_2, Ratio::new(3, 2)); assert_eq!(_NEG1_2, Ratio::new(-1, 2)); + assert_eq!(_2, From::from(2)); } #[test]