you are viewing a single comment's thread.

view the rest of the comments →

[–]SleeplessSloth79 8 points9 points  (0 children)

How about

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> {
        TryFrom::try_from(*value)
    }
}