Short enum variant syntax in some cases

Absolutely. I think just that change would immediately be a huge help for being able to use structs for many-argument functions and help sidestep the named argument conversations.

In the mean time, for the Default case you can use a macro trick:

macro_rules! s {
    ( $($i:ident : $e:expr),* ) => {
        {
            let mut temp = Default::default();
            if false {
                temp // this tricks inference into working
            } else {
                $(
                    temp.$i = $e;
                )*
                temp
            }
        }
    }
}

fn main() {
    let x: Foo = s! { y: 4 };
    dbg!(x);
}

#[derive(Debug, Copy, Clone, Default)]
struct Foo {
    x: i32,
    y: i32,
}

https://play.rust-lang.org/?edition=2018&gist=d449857a1d6c317ed4fe16dc09c2dee7

5 Likes