Type casting

Cairo supports the conversion from one scalar type to another by using the into and try_into methods. The into method is used for conversion from a smaller data type to a larger data type, while try_into is used when converting from a larger to a smaller type that might not fit.

For example:

    let a_number: u32 = 15;
    let my_felt252 = 15;

    // Since a u32 might not fit in a u8 and a u16, we need to use try_into,
    // then unwrap the Option<T> type thats returned.
    let _new_u8: u8 = a_number.try_into().unwrap();
    let new_u16: u16 = a_number.try_into().unwrap();

    // since new_u32 is the of the same type (u32) as rand_number, we can directly assign them,
    // or use the .into() method.
    let _new_u32: u32 = a_number;

    // When typecasting from a smaller size to an equal or larger size we use the .into() method.
    // Note: u64 and u128 are larger than u32, so a u32 type will always fit into them.
    let _new_u64: u64 = a_number.into();
    let _new_u128: u128 = a_number.into();

    // Since a felt252 is smaller than a u256, we can use the into() method
    let _new_u256: u256 = my_felt252.into();
    let _new_felt252: felt252 = new_u16.into();

    // Note: usize is smaller than felt252, so we use try_into
    let _new_usize: usize = my_felt252.try_into().unwrap();
Last change: 2024-06-09, commit: 3fbfb60