Make illegal states unrepresentable: unions over flags, parse don't validate, smart constructors
#Name alternatives and tagged variants (Types & syntax) shows how to write a union; this entry uses unions and validated constructors to design a model in which nonsense combinations cannot be built. Rust has everything this needs. A tuple struct with a private field inside a module, `pub struct Email(String)`, can only be built outside the module through Email::parse, so every Email has been checked (E0603 otherwise). Enums replace groups of optional fields: Reach has Email, Phone and Both, so a contact with no way to reach it cannot exist, and a variant such as Verified { at } would do the same for a flag with a date. parse_contact turns loose input into a Contact or an error, and matching on (email, phone) makes the compiler confirm every combination is handled.
Complete program
Complete program.
In practice: implement validation as a constructor returning Result (or TryFrom<&str>), keep the field private, and make fields public only on types whose every value is valid. Let match exhaustiveness find the places to update when a new state is added.
Privacy is per module, not per type: code in the same module (here, inside mod contact) can still build Email directly.
Deriving serde::Deserialize (serde 1.x) on a validated type skips the constructor; use #[serde(try_from = "String")] to route through TryFrom.