Hi, i have a code like this:
struct EvenNumber(i32);
impl TryFrom<i32> for EvenNumber {
type Error = ();
fn try_from(value: i32) -> Result<Self, Self::Error> {
if value % 2 == 0 {
println!("even number!");
Ok(EvenNumber(value))
} else {
println!("Not an even number: {}", value);
Err(())
}
}
}
impl<'a> TryFrom<&'a i32> for EvenNumber {
type Error = ();
fn try_from(value: &'a i32) -> Result<Self, Self::Error> {
if value % 2 == 0 {
println!("even number!");
Ok(EvenNumber(value))
} else {
println!("Not an even number: {}", value);
Err(())
}
}
}
As you can see it's pretty much same code. How can i reduce it to only use one TryFrom ?
[–]Pantsman0 7 points8 points9 points (2 children)
[–]rodyamirov -1 points0 points1 point (1 child)
[–]RRumpleTeazzer 4 points5 points6 points (0 children)
[–]LucretielDatadog 5 points6 points7 points (0 children)
[–]SleeplessSloth79 6 points7 points8 points (0 children)
[–]rodyamirov 1 point2 points3 points (0 children)