Rust field `dummy` of struct is private

I'm trying to create a struct like this:

DeviceCapabilities {
    max_transmission_unit: self.mtu,
    ..DeviceCapabilities::default()
}

Here's the struct definition:

#[derive(Debug, Clone, Default)]
pub struct DeviceCapabilities {
    pub max_transmission_unit: usize,

    pub max_burst_size: Option<usize>,

    pub checksum: ChecksumCapabilities,

    /// Only present to prevent people from trying to initialize every field of DeviceLimits,
    /// which would not let us add new fields in the future.
    dummy: ()
}

on the line DeviceCapabilities::default() of ..DeviceCapabilities::default() I get the following error:

field `dummy` of struct `smoltcp::phy::DeviceCapabilities` is private

First of all, why the field dummy is being filled, if the second one is max_burst_size?

Actually the piece of code that instantiates the struct is from a library. There it works but in my library it does not. I understand that field dummy is private as it has no pub, but why it works in the other library?

Also what the .. means? Maybe important to understand what's happening

If it works in your other library, then that library can probably access the private field from where it was called. The .. means the following:

let def = DeviceCapabilities::default();
DeviceCapabilities {
    max_transmission_unit: self.mtu,
    max_burst_size: def.max_burst_size,
    checksum: def.checksum,
    dummy: def.dummy,
}
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.