Anonymous Struct Parameters: Named Arguments are just Structs

There's been a recent round of posts on named arguments in Rust: Botahamec's Named and Optional Arguments are Awesome, Steve Klabnik's Arguing about arguments, and Matthias Endler's We Have Named Arguments at Home. I've taken my original thoughts from 2016 and written up a design that starts from Endler's premise: Rust already has a named, reorderable, defaultable construct, the struct. The design removes the ceremony makes using a struct this way less appealing.

TL;DR: it adds two pieces of syntax. struct { .. } is an anonymous struct type in parameter position, and _ { .. } is a struct literal or pattern whose type is inferred. Then some named argument sugar.

fn crop(img: &Image, struct { width: u32, height: Option<u32> = None, x: u32 = 0, y: u32 = 0, }) -> Image { .. } crop(&img, _ { width: 200, .. }); 

Read the full post:

1 post - 1 participant

Read full topic