How to initialize implicit struct?

I've seen this while browsing a pre-RFC:

I want to produce a similiar macro:

text_field.set_stylesheet(Arc::new(with! {
    heading_titles: hashmap! {
        1 => with! {
            font_size: Some(25.0),
            ..
        },
    },
    ..
}));

Is this possible without requiring Default::default()?

I have published a with_literal crate: with in with_literal - Rust

All initializations are limited to either Default::default() or a base object, which I think is fine for most uses, but someone might want to not use a base object for short structures, maybe?

It's a bit of a philosophical question: Default is pretty much just a trait that lets you define what the default value is, so what does it mean to "not require Default:: default"?

You can use some other way to get a value, but the only way to make that work across different types is basically another trait, so I guess it's up to you what it actually should look like!

3 Likes

I think OP meant not requiring Default::default() in the sense that the .. token at the end of the with! macro invocations stands as syntactic sugar for ..Default::default().

1 Like

That macro depends on Default::default() to have inference get you an instance. I'm not aware of any such trick that can be done for struct initializers directly, in order to allow not having defaults.

1 Like

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.