TryInto for Fallible Conversions

The TryInto trait is used for conversions that might fail. For example, converting from a larger integer type to a smaller one, or parsing a string into a number.

Unlike Rust which uses Result for fallible operations, Cairo uses Option to represent operations that might fail.

#[derive(Copy, Drop, Debug)]
struct EvenNumber {
    value: u32,
}

impl U32IntoEvenNumber of TryInto<u32, EvenNumber> {
    fn try_into(self: u32) -> Option<EvenNumber> {
        if self % 2 == 0 {
            Some(EvenNumber { value: self })
        } else {
            None
        }
    }
}

fn main() {
    // Direct conversion with TryInto
    let even: Option<EvenNumber> = 8_u32.try_into();
    println!("{:?}", even);

    let odd: Option<EvenNumber> = 5_u32.try_into();
    println!("{:?}", odd);
}