The "autocomplete" syntax for structs throws me an error that doesn't help

I created an exact example code of my scenario. I'm trying to use the syntax for "autocompleted" fields that weren't defined explicitly in a new struct declaration. However, cargo throws me an error that doesn't really help much and it drives me nuts that I can't understand whats happening, however the example I created is working exactly as intended. I need to know what am I missing.

This is the example with the crates used:

use typed_builder::TypedBuilder;
use serde_derive::Serialize;
use serde_derive::Deserialize;

#[derive(Clone, Debug, Default, TypedBuilder, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
struct SomeStruct {
    #[builder(default)]
    #[serde(skip)]
    field1: Option<bool>,
    #[builder(default)]
    field2: Option<u32>,
}

fn something(arg: Option<SomeStruct>) {
    let var = SomeStruct {
        field1: Some(true),
        ..arg.unwrap_or_default()
    };
    dbg!(var);
}

fn main() {
    something(None);
}

From toml:

serde = "1.0.116"
serde_derive = "1.0.116"
typed-builder = "0.7.0"

So as I said the above is working, however in my code which is exactly structured like that, I'm getting:

error[E0639]: cannot create non-exhaustive struct using struct expression
    |
266 |           let var = SomeStruct {
    |  _________________________^
267 | |             field1: Some(true),
268 | |             ..arg.unwrap_or_default()
269 | |         };
    | |_________^

If it matters the count of fields in the struct is different in the real code.

The error message is trying to say that it's forbidden to combine #[non_exhaustive] and ..default features together.

Either remove #[non_exhaustive], or create the struct using a different syntax (e.g. set the field value on arg).

How come the example above is working then if that's the case...? Initially I thought the same thing, doesn't seem to be that however.

Ah, I see. Maybe this restriction has been lifted in a later version of Rust.

Make sure you have up to date version of Rust:

rustup update

If you got an old one from a Linux distro, uninstall it. And then switch to https://rustup.rs

1 Like

Yes, I am with the newest version and it still doesn't work. The example above by the way is created in a separate test project with the same compiler.

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.