• soulsource
    link
    fedilink
    English
    arrow-up
    8
    ·
    1 year ago

    Another option that the author didn’t mention: Newtypes.

    There’s a pretty neat example for this in Rust By Example: https://doc.rust-lang.org/rust-by-example/generics/new_types.html

    If you don’t factor out the instantiation of the parameters, you get pretty close to having named parameters, actually:

    struct Weeks(u32);
    struct Days(u32);
    
    fn add_weeks_to_days(weeks : Weeks, days: Days) -> Days {
        let w = weeks.0;
        let d = days.0;
        Days(7*w+d)
    }
    
    fn main(){
        let Days(r) = add_weeks_to_days(Weeks(5), Days(3));
        println!("5 weeks and 3 days equals {r} days.");
    }
    

    (Playground)

    It is a little bit more work when writing the API, and the caller has to type a tiny bit extra when calling the function, but it also removes all ambiguity regarding the order of the funtion parameters. If also used on the return type, we suddenly get “named” return values too.