Struct update syntax and named parameters

I wish to use the struct update syntax alongside the format macro, for example:

struct Foo {
    x: String,
    y: String
}

fn main() {
    let f = Foo {
        x: "cool".to_string(),
        y: "nice".to_string()
    };
    let s = format!("X: {x}, Y: {y}", ..f);
}

Is there a way of filling out format's named parameters like this?

No, that would require a new feature.

If RFC 2795 stabilizes, you'll be able to write

    let x = "cool".to_string();
    let y = "nice".to_string();
    let s = format!("X: {x}, Y: {y}");

But you can't use expressions like f.x and macro hygiene is a barrier to getting around it.

1 Like

Ah, I really like the macro hygiene solution! Would it be possible to create a generic macro that takes any struct - perhaps using something similar to this? I am a little out of my depth when it comes to macros.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.