Why define errors as struct of unit type? e.g.: AddrParseError(())


I’m wondering why std::net::AddrParseError is defined as a newtype containing the unit type:

pub struct AddrParseError(());

This means inside the net module it can create errors as AddrParseError(()) but outside the module you can’t (AFAICT) create AddrParseErrors, even though it’s public, e.g. see playground example here.

Why is it done this way, and is this how I should make my own error types?


I think this is for the flexibility to allow, in the future, to add fields to it. If you make it a unit struct then it can be constructed externally and you cannot add fields without breaking backwards compatibility.