addtition: tryfrom and from
This commit is contained in:
25
src/lib.rs
25
src/lib.rs
@@ -160,13 +160,16 @@ impl fmt::Display for Fraction {
|
|||||||
|
|
||||||
impl From<i32> for Fraction {
|
impl From<i32> for Fraction {
|
||||||
fn from(value: i32) -> Self {
|
fn from(value: i32) -> Self {
|
||||||
todo!()
|
Fraction {
|
||||||
|
num: value.into(),
|
||||||
|
den: 1,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<i64> for Fraction {
|
impl From<i64> for Fraction {
|
||||||
fn from(value: i64) -> Self {
|
fn from(value: i64) -> Self {
|
||||||
todo!()
|
Fraction { num: value, den: 1 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,7 +177,14 @@ impl TryFrom<(i32, i32)> for Fraction {
|
|||||||
type Error = FractionError;
|
type Error = FractionError;
|
||||||
|
|
||||||
fn try_from(value: (i32, i32)) -> Result<Self, Self::Error> {
|
fn try_from(value: (i32, i32)) -> Result<Self, Self::Error> {
|
||||||
todo!()
|
if value.1 == 0 {
|
||||||
|
return Err(FractionError::ZeroDenominator);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Fraction {
|
||||||
|
num: value.0.into(),
|
||||||
|
den: value.1.into(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,7 +192,14 @@ impl TryFrom<(i64, i64)> for Fraction {
|
|||||||
type Error = FractionError;
|
type Error = FractionError;
|
||||||
|
|
||||||
fn try_from(value: (i64, i64)) -> Result<Self, Self::Error> {
|
fn try_from(value: (i64, i64)) -> Result<Self, Self::Error> {
|
||||||
todo!()
|
if value.1 == 0 {
|
||||||
|
return Err(FractionError::ZeroDenominator);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Fraction {
|
||||||
|
num: value.0,
|
||||||
|
den: value.1,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user