24
you are viewing a single comment's thread
view the rest of the comments
[-] soulsource@discuss.tchncs.de 8 points 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.

this post was submitted on 06 Jul 2023
24 points (92.9% liked)

Rust

6005 readers
8 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS